OdieO
OdieO

Reputation: 6994

'Undeclared Identifier' error with defined constants

My defined constants are giving me 'undeclared identifier' issues. I have them in a Constants.h file that I am including in my .pch file. I thought it might be something with my .pch file, however, if I delete it from there and try #import it in one of the classes where I need one of the constants, then I still get an 'undeclared identifier' error.

If I take each #define line and put them at the top of the .m class file directly, they work. So my syntax is correct.

So it's something with the .h file itself, but I have no idea what.

//
//  Constants.h

// Sliding Drawer
#define kOffscreenX 320    // X coord when Sliding Drawer is offscreen
#define kVisibleX 40       // X coord when Sliding Drawer is visible

// WordlistButton
#define kNumScores 3

// Fonts
#define kMessageFontSize 14
#define kScoreFontSize 10

Upvotes: 0

Views: 4854

Answers (2)

andreas-supersmart
andreas-supersmart

Reputation: 4294

I found this thread due to an other error upper case parameter in my define statement. I solved it for my issue with lower casing:

#define MSB(BTvalue) ((uint8_t) (BTvalue >> 8))  //threw this error

changing BTvalue to just value with lowercase parameter made me happy

#define MSB(value) ((uint8_t) (value >> 8)) 

Upvotes: 0

Sulthan
Sulthan

Reputation: 130072

It's impossible to see the error only from this piece of code. Preprocessor tends to create very messy things, especially when there are circular imports involved.

You can try to delete the current compiled version of the header, note it's not in the derived data folder, it's in XCode's cache (see Project -> Build Setttings -> Precompiled Headers Cache Path).

However, if you have tried to import Constants.h directly and it didn't work, the problem may be somewhere else.

Are you sure there is only 1 file called Constants.h? Note you should use a prefix for your files (e.g. SAConstants.h if Smooth Almonds is your name) to avoid collision with Apple's headers or headers of the libraries you are using.

If you import the header directly, go to the .m file and tap on Product -> Generate Output -> Preprocessed File and find Constants.h import in it. Is it your header?

By the way, there is a nice article about avoiding this kind of things in precompiled headers http://qualitycoding.org/precompiled-headers/

Upvotes: 3

Related Questions