Help Please

Started by
6 comments, last by Indy 23 years, 1 month ago
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
Indy
Advertisement
The problem that you are having is that the it is trying to include "main.h" twice so things are getting repeated.

The way to solve this is to put in your headers:
  #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
This sounds like the ifdef stuff

Say you have a file called GoboFraggle.h, then

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

Gee Brain, what we gonna do tonight?
you can also put ''#pragma once'' at the top of the header
It must be me, still no luck. Tried ALL the options given.


Is there a option that must be set in VC++6?
Indy
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^_^)
I am also having a similar problem :|

#ifndef THISHEADER
#define THISHEADER

//Header stuff
// 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 ??
~~~º¥º -
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
------------------------------"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'."-RemZirem Software

This topic is closed to new replies.

Advertisement