🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

#include file trouble

Started by
2 comments, last by Fuzz 24 years ago
I have a problem where lots of files in my project need to include a specific class. However some of these files have functions which have a pointer to that class as a parameter. Now, because of this, I need to put the #include "classname.h" command inside the header file of that file instead of its .cpp file. However due to this I get "redifinition of classname" errors. Is there a way around this?
Advertisement
Do this in your header files :

#ifndef __SOMETEXT__
#define __SOMETEXT__

class CMyClass
{
....
};

#endif

You can replace __SOMETEXT__ with whatever you want, just make sure its different for each header file. This will prevent classes from being defined multiple times.

Nate
http://nate.scuzzy.net
you don''t need to include the definition of a class just to declare something that uses a pointer to that class. you can just forward declare the class by doing:

class CFoo;

if you have CBar in Bar.h which has a funcion that takes a CFoo, do this:


#ifndef _BAR_H_
#define _BAR_H_

class CFoo;

class CBar
{
public :
void Init(CFoo* pFoo);
};

#endif

thanks for all your help.. Its funny, cause I''m at uni learning this stuff, but they only cover the real basics, and stuff like this that I imagine would be quite common they dont even mention.. ah well. Thanks again

This topic is closed to new replies.

Advertisement