Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wanting2learn

macrumors newbie
Original poster
Aug 19, 2010
13
0
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
 

ianray

macrumors 6502
Jun 22, 2010
452
0
@
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?).
Yes.

If I then take out the 'const' keyword from all variables then I still get the same error??
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
 

wanting2learn

macrumors newbie
Original poster
Aug 19, 2010
13
0
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
 

ianray

macrumors 6502
Jun 22, 2010
452
0
@
My iPhone app is a View Based Application, is there a way to use Objective C++ instead of objective C in this project??
Thanks

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.