Archived

This topic is now archived and is closed to further replies.

endasil

Using constants in header files

Recommended Posts

endasil    138
Okay, i''m having a problem using constants in header files and would like to know why this isn''t working. In my file MapClass.cpp i have: const MAP_HEIGHT = 1000; In MapClass.h i have told the compiler that that variable is defined in another file by doing a: extern const MAP_HEIGHT; My problem is that when i then try to use these constants in the MapClass.h in this way: struct MapZoneStruct Zone[11][MAP_HEIGHT]; it throws me a error C2057: expected constant expression Why does it work like this?

Share this post


Link to post
Share on other sites
Peon    276
Honestly, I don''t know a thing about const, but for things like that, I typically #define MAP_HEIGHT #, #define MAP_WIDTH # (assuming all of your maps are the same size)

Share this post


Link to post
Share on other sites
Tac-Tics    197
Short answer:
Define all constants in your H files. Do no use `extern`... do not collect $100.

Long answer:
When you say "const varname" you aren''t technically creating a variable. You''re creating a macro.

When you create a variable, you need to export it with the `extern` keyword so the linker knows to look up the variable in a your object files.

However, when you use `const` or `#define` to create a fake "variable", what the compiler (the pre-processor) does is go through your source code and replaces the variable name with the value.

If you try to use `extern`, the linker looks for a variable, but can''t find one because there is none! All those MAP_HEIGHT''s have been replaced with the immediate value 1000!

/¯\_/¯\_/¯\_/¯\_/¯\_/¯\
"You TK''ed my chicken!"
\_/¯\_/¯\_/¯\_/¯\_/¯\_/

Share this post


Link to post
Share on other sites
Lord Bart    226
Hello endasil,

The problem might be you can''t have the header file have extern const MAP_HEIGHT declear but it has no value and then your trying to create struct MapZoneStruct Zone[11][MAP_HEIGHT] father down in this same header file. This is a no-no. MAP_HEIGHT has no value in this header file.

Now you might be able to declare extern const MAP_HEIGHT = 1000;
This give compile what it need a actual value for sizing the static array.

Or move the define of Zone to the cpp file. and just declare it in header file. As a pointer to MapZoneStruct.

Tac-Tics is wrong on the
"When you say "const varname" you aren''t technically creating a variable. You''re creating a macro."

You are creating a variable which take space at runtime, you can take the address of it, you can do a sizeof on it, only thing you can''t do is change the value once it set, and you debug your program can actual output it value in a watch window.
This can''t be done for a macro.
const are the way to go if you are going to do any kind of debugging.

Lord Bart

Share this post


Link to post
Share on other sites
capn_midnight    1707
uhm, how about we put a data type on that, eh?

you have "const MAP_HEIGHT = 100;" I can see that is an int, but the compiler don''t know. try "const int MAP_HEIGHT=100;"

this IS a variable. It is NOT a macro.

THIS is probably what Tac-Tic thought was a macro:
#define MAP_HEIGHT 100

using #define will tell the C/C++ Pre-processor (a nasty little bitch that is the main contributor to code confuscation) to replace every instance of "MAP_HEIGHT" with "100". That is why you don''t put a ";" at the end of the define, because it would replace "MAP_HEIGHT" with "100;", and you don''t want that.


Do you use your powers for good or for awesome?
|My site | Association of Computing Machinery

Share this post


Link to post
Share on other sites