Skip to main content
GameDev.net gamedev.net
🔒 Locked

Header files

Started by bvs May 11, 2004 at 5:46 AM 4 replies 600+ views
Original Post
bvs
bvs
Kind of a newbie question... but I guess I''ve never managed to learn the correct answer. What information is suppossed to be contained in header files? Is it just function definitions or should put all constant variables in there too? If I DO try to put constant variables in header files I often run into linking errors (on bigger projects)
RaptorZero
RaptorZero
Header files should contain #includes, #defines, functions definitions, classes declarations, structures and some other things

If you declare variables in a header file and include it in two or more modules (what you will certainly do on bigger projects) you will get linkin errors because it would be the same as declaring the same variable in every module of the project

If you want to declare constant variables in it, #define them because defines are not actualy variables, they are substituted by the compiler when you build your project...

Ok?

error C2065: 'signature' : undeclared identifier
Arild Fines
Arild Fines
quote:
Original post by RaptorZero
If you declare variables in a header file and include it in two or more modules (what you will certainly do on bigger projects) you will get linkin errors because it would be the same as declaring the same variable in every module of the project

If you want to declare constant variables in it, #define them because defines are not actualy variables, they are substituted by the compiler when you build your project...


consts are not subject to this restriction. You can put const variables in a header file without the linker complaining.

And const > #define.



--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
nonpop
nonpop
Clicky
You can use constants in headers (I think):

int const ARBITRARY_INTEGER = 5;
float const PI = 3.141592654f;
char const* const VERSION = "3.7 alpha"; // Note there are TWO ''const''s

bvs
bvs
Why is const > #define?

What are the advantages?
Zipster
Zipster
const allows the compiler to perform type-checking. Which is a great thing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.