Archived

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

Fuzz

#include file trouble

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites