# Header Files - Declare function prototypes and structures separate?

This topic is 2833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been getting some strange linking errors lately about the same functions being declared in multiple objects. Some quick research revealed this to be nested includes of header files, but I have all my header files #ifndef guarded. I was just wondering if maybe you weren't supposed to declare structures, enums, etc. in the same header file as function prototypes?

Example: this is what I do
a.h
 #ifndef A_H_ #define A_H_ typedef struct { int x; int y; // ... } A; void NewA(A *a); void DeleteA(A *a); #endif 

Is this what I should do?
A_structs.h
 #ifndef A_STRUCTS_H_ #define A_STRUCTS_H_ typedef struct { int x; int y; // ... } A; #endif 

A_functions.h
 #ifndef A_FUNCTIONS_H_ #define A_FUNCTIONS_H_ #include "A_structs.h" void NewA(A *a); void DeleteA(A *a); #endif 

##### Share on other sites
[color="#1C2837"] I was just wondering if maybe you weren't supposed to declare structures, enums, etc. in the same header file as function prototypes?[/quote]
[color="#1C2837"] This is not a requirement and if this is C you can completely leave out the function parameters in the declaration if you so desire.

In your example you have another two choices.
1 Forward declare the type
struct A;
2 Include the definition of the structure in the same TU.

##### Share on other sites
You cannot define anything in headers (there are exceptions, like const types, or templates) --that will cause compiler errors. You should use headers for declarations only and the cpp files to define anything. For example
<code>
#ifndef blar
#define blar

void Food(); // this is a declaration

class foodforme{
public:
int foodserved;
void running();//

};

int counter; // <---------------THIS IS BAD, will cause compiler errors because it is a definition
extern int goodexample; // <----------------This is fine, it is a declaration. THIS IS THE PROPER WAY TO CREATE A GLOBAL VARIABLE,
//Do not use static. If you did, you would get different copies of goodexample each time the header was included.
#endif

/////////cpp file

int goodexample;//<-----Now, You define it in the cpp file and there are no compile errors

#include "blar.h"

void Food(){ // this is a definition
std::cout<<"I like food"<<std::endl;
}
</code>

using typedef in that manner always Urked me! Try using just a struct A { int x, y; } There is no need for the typedef.

##### Share on other sites
Thanks for the replies, you all. By the way, smasherprog, your use of extern for defining global variables was clever. I'll definitely be using that from this point on =) As for the typedef, I often code in C rather than C++ and don't want to type the "struct" keyword before every structure that I'm using.

##### Share on other sites

Thanks for the replies, you all. By the way, smasherprog, your use of extern for defining global variables was clever. I'll definitely be using that from this point on =)

It is standard, and what you are expected to do (assuming that you really need a global variable in the first place). Of course, if outside code doesn't need to know about this global (say for example that it only interacts with the global via the functions listed in the header), then you don't need to, and shouldn't, mention it in the header at all.

##### Share on other sites
using typedef in that manner always Urked me! Try using just a struct A { int x, y; } There is no need for the typedef.

You're thinking about C++, in C it's required. The alternative is to type "struct A* a;" all the time, which is tiring.

@topic I've found that some compilers can complain about recursive includes even when include guards are present, giving unintelligible errors. I usually try to make sure all headers are dependent in a hierarchy through forward-declarations to avoid the issue altogether.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×