Jump to content
  • Advertisement
Sign in to follow this  
deus.ex.nova

[C++] #include in header, source, or both?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just split my project's class declarations and implementations to their respective header and source files, and then I placed the #include's in the source files. However, the IDE is telling me that some of the functions I've been using ( e.g. glClear() ) are undefined. I made sure that opengl32.lib was included in the linker, but that still doesn't resolve the problem. I'm thinking that perhaps it's where I am putting my #include statements. Inferring from some example code, I thought #include statements went into the cpp file only, but do I also have to put it into the header file? FYI I am using VS2010. Thanks in advance.

*EDIT* I'm getting many compiler errors that say: ...int assumed. C++ does not support default-int.

Share this post


Link to post
Share on other sites
Advertisement
Rule of thumb: include what you need, and no more

If you need to include another header to make you header idempotent, then you need to.

Share this post


Link to post
Share on other sites

Rule of thumb: include what you need, and no more

If you need to include another header to make you header idempotent, then you need to.


Sorry, what do you mean by idempotent? Also, I edited my first post that may hint to what my problem may be. Thanks, for your help.

Share this post


Link to post
Share on other sites
If the header file refers to something declared in some other header file, you need to either include that other header file or use a forward declaration (ignore this last bit for the time being if you don't know what a forward declaration is).

It is good practice to make the first line of foo.cpp be `#include <foo.h>', so the compiler can verify that you didn't forget any dependencies in the header file.

Share this post


Link to post
Share on other sites

[quote name='Bregma' timestamp='1316455550' post='4863443']
Rule of thumb: include what you need, and no more

If you need to include another header to make you header idempotent, then you need to.


Sorry, what do you mean by idempotent? Also, I edited my first post that may hint to what my problem may be. Thanks, for your help.
[/quote]

"Idempotent" is pedantic way of saying that doing something twice has the same effect as doing it once. But I don't understand what he said either, so he probably thinks it means something else, like "self sufficient".

Share this post


Link to post
Share on other sites

If the header file refers to something declared in some other header file, you need to either include that other header file or use a forward declaration (ignore this last bit for the time being if you don't know what a forward declaration is).

It is good practice to make the first line of foo.cpp be `#include <foo.h>', so the compiler can verify that you didn't forget any dependencies in the header file.


Yeah I have my header file as the first include in the source file, then the rest are includes from either other headers in my project or external ones. I don't have any includes in my source's respective header file though.

Share this post


Link to post
Share on other sites
What is the first error your compiler gives you? It's probably somewhere where you are using something that is declared in another header file. You need to include that header file from the one that uses it.

Post the header file that is giving you trouble if you still can't figure it out.

Share this post


Link to post
Share on other sites

[color=#1C2837][size=2]I'm getting many compiler errors that say: ...int assumed. C++ does not support default-int.
[color=#1C2837][size=2][/quote]
[color=#1C2837][size=2]

[color=#1C2837][size=2]I think this is a side effect of using a type that is not recognized by the compiler.
[color=#1C2837][size=2]

[color="#1c2837"]If you are using any types or functions from OpenGL in your header file, you need to include OpenGL headers there.
[color="#1c2837"]

[color="#1c2837"]Also, if you are using inclusion guards (which you should), make sure you didn't accidentally used the same one for different headers...

Share this post


Link to post
Share on other sites
What I mean is that you header file needs to be self-sufficient. It needs to include everything it needs to be compilable. I should be able to create a .c (or .cpp) file that has one line consisting of an include directive to pull in your header file and it should still compile (granted, I would not expect it to link).

If you're getting the message about "int assumed" it's a sure-fire indication that you're missing some necessary includes in your header file.

Remember that the preprocesor just does text substitution, and that a header file is just textually substituted in another file. There's no point in including anything in a header file that isn't supposed to get included in two different translation units (.cpp files), and no point in creating a header file that does not get included in at least two different translation units.

Share this post


Link to post
Share on other sites

What is the first error your compiler gives you? It's probably somewhere where you are using something that is declared in another header file. You need to include that header file from the one that uses it.

Post the header file that is giving you trouble if you still can't figure it out.




Error 1 error C2146: syntax error : missing ';' before identifier 'stars'
Here is the code that it is referring to (I am still learning, so feel free to make any other corrections/suggestions to improve my code):
#ifndef GLGAMELOOP_H_
#define GLGAMELOOP_H_


class GameLoop
{
public:

enum Object { DotField, Ball };

GameLoop(); // GameLoop constructor
~GameLoop(); // GameLoop destructor

int UpdateContent();

int DrawScene();


private:

// create class objects
DotField stars; // 1st error is occurring here
Sphere ball;
ObjectMotion ballMotion;

void SetRenderingContext( const int &objectEnum ); // i.e. Lighting

bool IsBallInViewport(); // check if ball is in the viewport

void RegisterKeys();
};


#endif





[color="#1C2837"]I'm getting many compiler errors that say: ...int assumed. C++ does not support default-int.
[color="#1C2837"]


[color="#1C2837"]I think this is a side effect of using a type that is not recognized by the compiler.

[color="#1c2837"]If you are using any types or functions from OpenGL in your header file, you need to include OpenGL headers there.

[color="#1c2837"]Also, if you are using inclusion guards (which you should), make sure you didn't accidentally used the same one for different headers...
[/quote]

There are no OpenGL functions in my header files, and the class that's giving me the first set of errors is fine as far as inclusion guards go. Though I just realized I missed some semicolons when I was splitting files. ( ._.) I still have the same error occurring even after I fixed those semicolons.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!