error: initializer element is not constant

Discussion in 'iOS Programming' started by wanting2learn, Oct 23, 2010.

  1. wanting2learn macrumors newbie

    Joined:
    Aug 19, 2010
    #1
    Hopefully this is my last question of the day...:)

    I now have:

    Code:
    #define WM_USER 0x0400
    const UINT MSGID_FIRST	= WM_USER + 301;
    
    const DWORD MSGID_NTLX_FIRST		= MSGID_FIRST; <-- error
    
    The error is:
    error: initializer element is not constant

    I'm thinking this error is because I'm initializing a const variable with another variable which the compiler is clamping down on (Is this right?).

    If I then take out the 'const' keyword from all variables then I still get the same error??

    Can you help me??

    My last compiler was Visual Studio so It must have been pretty lenient on the rules?

    Thanks
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Yes.

    Yes.

    In C (and therefore Objective-C) initializers must be constant values.
    Code:
    static int theUltimateAnswer = 42;  // OK
    const unsigned magic = 0xaBad1dea;  // OK
    double fiddleFactor = 3.141;        // OK
    int foo = 10 + 2;                   // OK
    int bar = foo;                      // NOT OK
    
    C++ (and therefore Objective-C++) removes this restriction. Some programmers use C++ simply as a better C (ignoring all of it's other features) for this very reason :D
     
  3. wanting2learn thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #3
    thanks

    Ok That makes sense, thanks.

    Is there a way I can work around this?
    You see I have a header file that has about 200 of these definitions like so:

    Code:
    const UINT MSGID_NTLX_FIRST		= MSGID_FIRST;
    const DWORD MSGID_NTLX_LAST			= MSGID_NTLX_FIRST			+ 49;
    
    // MSG_VIDREQUEST, local search
    const DWORD MSGID_VIDREQUEST_FIRST	= MSGID_FIRST				+ 50;
    const DWORD MSGID_VIDREQUEST_LAST	= MSGID_VIDREQUEST_FIRST	+ 49;
    
    // MSGID_VIDTRANSFER, video image returned from remote site
    const DWORD MSGID_VIDTRANSFER_FIRST	= MSGID_FIRST				+ 100;
    const DWORD MSGID_VIDTRANSFER_LAST	= MSGID_VIDTRANSFER_FIRST	+  49;
    
    // Time sync messages
    const DWORD MSGID_TIMESYNC_FIRST	= MSGID_FIRST				+ 150;
    const DWORD MSGID_TIMESYNC_LAST		= MSGID_TIMESYNC_FIRST		+  49;
    //blah blah
    //blah blah
    
    My iPhone app is a View Based Application, is there a way to use Objective C++ instead of objective C in this project??

    Thanks
     
  4. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #4
    You can add files with extension '.mm' to your XCode project -- and XCode recognizes these as Objective-C++. (This of course implies that you have to rename any and all modules that include that header file to have extension '.mm'.)

    Alternatively, you could transform that header into an enumeration. (Use perl/awk/sed to do the dirty work.)

    Alternatively, you could transform that header into a series of #define's, but as noted earlier in the thread such an approach is recognized as ugly and unmaintainable.
     

Share This Page