#### Archived

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

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

## Recommended Posts

I definately give up!! I need (gulp) help. OK, so i''m gonna make a game. I want to split my program into smaller bits of code to make it easier to read. So I setup a "main.h" file with all my prototypes and my #includes.. I''m using the CDX GDK, so my "main.h" has all the includes.. I need for the CDX library. So far everything good. I''m now gonna setup one of the .cpp files, lets say its the one that setups the graphics mode. I call it "mode.cpp", this has the first line #include "main.h" which you would expect, so nothing wrong so far (...right??). Lets just assume that I have a "main.cpp" file, which contains all the necessary windows class setup. I also have a line at the top of "main.cpp" #include "main.h".....ok hitting CTRL F5 (i''m using VC++6) So why the f*** do I get errors galore..like "xxxxxx is already defined in main.obj" Does that suggest i''m including something more that once? If I take out the #include "main.h" from any of the files I get a "xxxxx function not found" I know one thing, the answer lies in the "ifndef...." keywords somewhere. Problem is I don''t know what the hell to do, apart from having one real long "main.cpp" file that has all the code. Last time I looked that was bad practice. If anyone can give me any help I will appreciate it. In fact I will credit them in my game (promise) Thankyou for reading my plea.. Indy Indsoft

##### Share on other sites
The problem that you are having is that the it is trying to include "main.h" twice so things are getting repeated.

  #ifndef SOMELABEL#define SOMELABEL//Header stuff#endif

SOMELABEL is any valid variable name you want. It could be something like _HEADER_CHECK_

The way this works is that when you try to include the header it looks to see if the the variable has been defined. If not then define it and include the header. Then when you try to include it again the compiler sees the the variable has been defined which means that that header has already been included. The #ifndef will then fail and the header will not be repeated. Just put this type of thing in all your headers with different labels and you should be ok

-------
Andrew

##### Share on other sites
This sounds like the ifdef stuff

Say you have a file called GoboFraggle.h, then

  #ifndef GOBOFRAGGLEH#define GOBOFRAGGLEH.... declarations and other stuff..#endif

##### Share on other sites
you can also put ''#pragma once'' at the top of the header

##### Share on other sites
It must be me, still no luck. Tried ALL the options given.

Is there a option that must be set in VC++6?

##### Share on other sites
I do the same thing, and had the same problem, which is that you need to put "extern" before your variables in the .h file, and actually declare them in main.cpp (just copy from the header and remove the externs). Hope it works for you too^^

-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

##### Share on other sites
I am also having a similar problem :|

// data and protos
#endif

With this diclaration in the header file it should include the header only once for compilation and not generate a ...is already defined in xxx.obj .. why does this happen. It does not happen in some of my earlier projects. I think it must be somewhere in the project settings .. any pointers ??

##### Share on other sites
I have had this problem as well.

As mentioned you need to use the #ifndef/#define/#endif tags in the header. Furthermore, any variables declared there should be declared extern. I don''t know if this is the ONLY way to do it, just this is the only way I have figured out.

Example:
In the WinMain.h file

  #ifndef Main_H#define Main_H // Declare an instance of CBall extern CBall MyBigBall; // heh#endif

In the WinMain.cpp file (or any file that includes main.h)
  // Declare an instance of CBall CBall MyBigBall; // heh

Hope that helps. Let me know if you need more explanation. You should be able to get it though

------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."

-Rem

• ### Forum Statistics

• Total Topics
628654
• Total Posts
2984055

• 10
• 9
• 9
• 10
• 21