Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

avoiding multiple definitions of objects

This topic is 4834 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

Yes, yet another of my famous multiple def threads... (I _am_ learning!) Say I have some class, "foo". I have to have one global foo object. I am making a win32 api app, and the winproc for the main window is in a file called "winproc.cpp", included by "winproc.h", included by "main.cpp", which holds the WinMain function. If I define the foo singleton in either winproc.h or winproc.cpp, it gives me a multiple definition error. How can I avoid this? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I know it is stating the obvious but you are using inclusion guards in your header files right?

ace

Share this post


Link to post
Share on other sites
Do not declare objects or variables in header. Use inclusion guard and if you want global objects make it "extern" and declare it in some module.

Share this post


Link to post
Share on other sites
Don't include CPP files, add them to your probject (or makefile or whatever).

Then you have foo.h and foo.cpp, and in foo.h you declare the class and it's members, and in foo.cpp you implement the members. Also, in foo.h, at the end, you can have 'extern foo GlobalFooInstance;' and in foo.cpp you have 'foo GlobalFooInstance;'
Now you can access GlobalFooInstance from any cpp file that includes foo.h, and you'll get no errors about definining it multiple times.

Share this post


Link to post
Share on other sites
Yes - I've posted enough posts about the inclusion guard thing to last me a lifetime. I'm using them :)

The extern thing was no dice though. Could you give me a short example? I tried this:

global.h

#ifndef GLOBAL_HPP
#define GLOBAL_HPP

extern Foo foo_object;

#endif



global.cpp

#include "global.h"

Foo foo_object;



Still got the multiple def error, though...

Share this post


Link to post
Share on other sites
No reason why you should get an error about multiple definitions in that example. The problem must be elsewhere. How and where is Foo declared?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!