www.delorie.com/djgpp/bugs/show.cgi | search |
This program will not compile as C++. Compiler error for the "For (result=...) line is shown below. When I change the iostream and cout to regular C code (i.e. printf) compile is OK. // Chapter 2 - Program 1 #include <iostream.h> enum game_result {win, lose, tie, cancel}; main() { game_result result; enum game_result omit = cancel; for (result = win;result <= cancel;result++) { if (result == omit) cout << "The game was cancelled\n"; else { cout << "The game was played "; if (result == win) cout << "and we won!"; if (result == lose) cout << "and we lost."; cout << "\n"; } } } >gcc enum.cpp -Wall -lgpp -o enum.exe enum.cpp: In function `int main()': enum.cpp:11: no post-increment operator for type
After further work I have noticed that the problem in the FOR statement is the portion "result++". This statement compiles with a warning and executes if changed to "result=result+1".
My C++ reference book mentions: "A small but potentially important difference between C and C++ is that in C, a character constant is automatically elevated to an integer. In C++, it is not." It seems likely to me that this is the problem; under C the compiler is treating the enumeration as an integer, so the ++ operator works, but under C++ it treats it as an enumeration, and no ++ operator is defined. I don't see this as being a problem. The point of making a variable an enumeration rather than an integer is that it will be holding basically nonlinear values. You would normally deal with such a variable by using an if or switch statement, not by incrementing it.
webmaster | delorie software privacy |
Copyright © 2010 by DJ Delorie | Updated Jul 2010 |