passing #define constant as parameter

why does this not work?

in testApp.h:

#define PARAM_1 200;  

in testApp.cpp:

// ...  
void myFunc (int param) {  
	// ...  

i get these compile-time errors at the myFunc() call:
error: expected )' before ';' token error: expected primary-expression before ')' token error: expected;’ before ‘)’ token

i’m not sure what a “primary-expression” is, but my understanding of the #define directive is that it swaps all occurrences with the specified value before compilation, so i thought this would be equivalent to:


(which compiles and runs just fine.)


This is a subtle error, and it’s caused by the behaviour of the preprocessor. As you said, all that the the #define directive does is text replacement, but preprocessor commands don’t need to be terminated by a ;

So when you do:

#define PARAM_1 200;  

And then try to use it, the compiler sees:


Oops :slight_smile:


oy. very sneaky. thanks for the explanation! const it is.

so then, while we’re on the topic…what’s a good example of when one would want to use #define? i’m sure there are plenty, but i’ve never written in a language with a preprocessor before so it’s a new way of thinking for me.

ok, so…having trouble with const.
seems that i cannot declare a const member within a header file?

if i try to initialize the constant when declaring it (standard procedure in other languages i’ve written):

class testApp : public ofBaseApp {  
	// public declarations here...  
		const int PARAM_1 = 100;  

i get the error:
ISO C++ forbids initialization of member PARAM_1

if i don’t initialize the constant at declaration:

class testApp : public ofBaseApp {  
	// public declarations here...  
		const int PARAM_1;  

i get this error:
Uninitialized member ‘testApp::PARAM_1’ with ‘const’ type ‘const int’

i can put the declaration+initialization at the top of a source file, but it seems odd to have some members in the header and some in the source. am i missing something?

okok. one more reply to self.

i’m still not really clear on why a const member cannot be initialized within a header file, but i figured out that a static const member *can* be initialized within the header. i intended for these variables to be static const anyway, so that’s fine.

if i had to take a guess at why a const member can’t be initialized within a header file, i’d say (based on some confused googling) it’s because it violates ODR, tho i’m not quite sure how…

anyway, this works fine:

class testApp : public ofBaseApp {  
	// public declarations here...  
		static const int PARAM_1 = 100;  

maybe think about it like this: if you have something that is const, why wouldn’t you want it to be static?

if it wasn’t static, you would be making unnecessary copies of the data, one per instance of the class. since it isn’t changing, this would be overkill.

on the other hand, if you’re just interested in giving a default value to a member, you can do this with the constructor:

class MyObject {  
 int myVariable;  
 string myName;  
 MyObject() : myVariable(10), myName("default name") {