Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. constant expression in case

constant expression in case

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    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??

    A C E 3 Replies Last reply
    0
    • E Ernesto D

      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??

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      shouldn't this be:

      switch(num)
      {
      case SOME_INT:
      ATLTRACE("someint");
      break;
      default:
      // whatever
      };

      Cheers, Andy

      E 1 Reply Last reply
      0
      • A Andrew Quinn AUS

        shouldn't this be:

        switch(num)
        {
        case SOME_INT:
        ATLTRACE("someint");
        break;
        default:
        // whatever
        };

        Cheers, Andy

        E Offline
        E Offline
        Ernesto D
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • E Ernesto D

          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??

          C Offline
          C Offline
          Curi0us_George
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • C Curi0us_George

            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.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            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

            C G 2 Replies Last reply
            0
            • T toxcct

              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

              C Offline
              C Offline
              Curi0us_George
              wrote on last edited by
              #6

              Actually, I'm pretty sure that VC.NET2003 is as compliant with the standards as GCC.

              1 Reply Last reply
              0
              • E Ernesto D

                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??

                E Offline
                E Offline
                Ernesto D
                wrote on last edited by
                #7

                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!

                G 1 Reply Last reply
                0
                • E Ernesto D

                  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!

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #8

                  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;

                  1 Reply Last reply
                  0
                  • T toxcct

                    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

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    As much as I hate quoting Joan Rivers, oh grow up.


                    Software Zen: delete this;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups