Sign in to follow this  
Master Jake

Header Files - Declare function prototypes and structures separate?

Recommended Posts

Master Jake    100
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
[b]a.h[/b]
[code]
#ifndef A_H_
#define A_H_

typedef struct
{
int x;
int y;
// ...
} A;

void NewA(A *a);
void DeleteA(A *a);

#endif
[/code]

Is this what I should do?
[b]A_structs.h[/b]
[code]
#ifndef A_STRUCTS_H_
#define A_STRUCTS_H_

typedef struct
{
int x;
int y;
// ...
} A;

#endif
[/code]

[b]A_functions.h[/b]
[code]
#ifndef A_FUNCTIONS_H_
#define A_FUNCTIONS_H_

#include "A_structs.h"

void NewA(A *a);
void DeleteA(A *a);

#endif
[/code]

Share this post


Link to post
Share on other sites
magic_man    54
[quote][color="#1C2837"][size="2"] I was just wondering if maybe you weren't supposed to declare structures, enums, etc. in the same header file as function prototypes?[/quote][/size][/color]
[color="#1C2837"] [/color]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
[code]struct A;[/code]
2 Include the definition of the structure in the same TU.

Share this post


Link to post
Share on other sites
smasherprog    568
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>
/////Header file
#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 this post


Link to post
Share on other sites
Master Jake    100
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 this post


Link to post
Share on other sites
Zahlman    1682
[quote name='Master Jake' timestamp='1302987412' post='4799259']
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 =)[/quote]

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 this post


Link to post
Share on other sites
Vectorian    109
[quote name='smasherprog' timestamp='1302985899' post='4799249']using typedef in that manner always Urked me! Try using just a struct A { int x, y; } There is no need for the typedef.[/quote]
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.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this