constant expression in case
-
Hi all, i dont get it, heres some "sample" code to explain my problem: const int SOME_INT = 100; // notice the "const" at the start int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; gives me: error C2051: case expression not constant. BUT: #define SOME_INT 100; // using define instead int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; Gives me no trouble! WTF???? "case expression not constant"???? isnt SOME_INT clearly defined as constant in the first case??
-
Hi all, i dont get it, heres some "sample" code to explain my problem: const int SOME_INT = 100; // notice the "const" at the start int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; gives me: error C2051: case expression not constant. BUT: #define SOME_INT 100; // using define instead int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; Gives me no trouble! WTF???? "case expression not constant"???? isnt SOME_INT clearly defined as constant in the first case??
shouldn't this be:
switch(num)
{
case SOME_INT:
ATLTRACE("someint");
break;
default:
// whatever
};Cheers, Andy
-
shouldn't this be:
switch(num)
{
case SOME_INT:
ATLTRACE("someint");
break;
default:
// whatever
};Cheers, Andy
Sorry, the ":" after case is a mistake i made while writting the post ( edited the post and corrected it now), but even without it, if you try to compile the code you wrote, and assuming you declared SOME_INT as const int SOME_INT = 0; it will give you the error, but if you declare it like: #define SOME_INT 0 then it will work fine. any idea why?
-
Hi all, i dont get it, heres some "sample" code to explain my problem: const int SOME_INT = 100; // notice the "const" at the start int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; gives me: error C2051: case expression not constant. BUT: #define SOME_INT 100; // using define instead int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; Gives me no trouble! WTF???? "case expression not constant"???? isnt SOME_INT clearly defined as constant in the first case??
Are you using VC6? Because it compiles just fine in VC.NET2003 (once the default case is filled in). According to the standards, it should work, so either your compiler is breaking the standards, or what you've posted isn't quite the same as what you've been trying to compile.
-
Are you using VC6? Because it compiles just fine in VC.NET2003 (once the default case is filled in). According to the standards, it should work, so either your compiler is breaking the standards, or what you've posted isn't quite the same as what you've been trying to compile.
-
Curi0us_George wrote: According to the standards, it should work Yes it should, but with Microsoft, there's no more standard... :( they implement their compiler how they like... X|
TOXCCT >>> GEII power
Actually, I'm pretty sure that VC.NET2003 is as compliant with the standards as GCC.
-
Hi all, i dont get it, heres some "sample" code to explain my problem: const int SOME_INT = 100; // notice the "const" at the start int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; gives me: error C2051: case expression not constant. BUT: #define SOME_INT 100; // using define instead int num=0; switch(num) { case SOME_INT: ATLTRACE("someint"); break; default: // whatever }; Gives me no trouble! WTF???? "case expression not constant"???? isnt SOME_INT clearly defined as constant in the first case??
Heres the REAL deal in a file (appconsts.cpp)
#include "appconsts.h" const int COLID_PLV_NAME = 0 const int COLID_PLV_PHONE = 1 const int COLID_PLV_REP = 2
and in another file (appconsts.h) wich is included everywhere...extern const int COLID_PLV_NAME; extern const int COLID_PLV_PHONE; extern const int COLID_PLV_REP;
and finally, in the LVN_COLUMNCLICK handler of one of my views (appconsts.h included here of course)...switch(ColIdx) { case COLID_PLV_NAME: // do something here break; case COLID_PLV_PHONE: // more code here also break; case COLID_PLV_REP: // and more code break; default: return 0; // we dont care about other cols. }
Looks right no?, but it gives me... C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(204) : error C2051: case expression not constant C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(207) : error C2051: case expression not constant C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(210) : error C2051: case expression not constant i just dont get it! -
Heres the REAL deal in a file (appconsts.cpp)
#include "appconsts.h" const int COLID_PLV_NAME = 0 const int COLID_PLV_PHONE = 1 const int COLID_PLV_REP = 2
and in another file (appconsts.h) wich is included everywhere...extern const int COLID_PLV_NAME; extern const int COLID_PLV_PHONE; extern const int COLID_PLV_REP;
and finally, in the LVN_COLUMNCLICK handler of one of my views (appconsts.h included here of course)...switch(ColIdx) { case COLID_PLV_NAME: // do something here break; case COLID_PLV_PHONE: // more code here also break; case COLID_PLV_REP: // and more code break; default: return 0; // we dont care about other cols. }
Looks right no?, but it gives me... C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(204) : error C2051: case expression not constant C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(207) : error C2051: case expression not constant C:\DEV\PROJECTS\BOLETAJE\1.0_CURRENT\ProvidersView.cpp(210) : error C2051: case expression not constant i just dont get it!The case expression must be constant at compile time. The circumstances you've described do not let the compiler know the value of the case expressions until link time.
Software Zen:
delete this;
-
Curi0us_George wrote: According to the standards, it should work Yes it should, but with Microsoft, there's no more standard... :( they implement their compiler how they like... X|
TOXCCT >>> GEII power
As much as I hate quoting Joan Rivers, oh grow up.
Software Zen:
delete this;