Archived

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

#include problem

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

Hi guys. I get an error while compiling two classes which have included each other. here my code CLASS 1: #ifndef TEXTURE_H #define TEXTURE_H #include "graphics.h" class Texture { .... Graphics* getGraphics(); one method in my class }; #endif CLASS 2: #ifndef GRAPHICS_H #define GRAPHICS_H #include <d3d8.h> #include <d3dx8.h> #include <dinput.h> #include "texture.h" class Graphics { . . . Texture* getCurrentTexture(); one method in graphics }; #endif but this doesn''t work. I don''t know why? Perhaps someone can help me;

Share this post


Link to post
Share on other sites
Your problem is that when the fisrt class, in either case, is declared it references the second class which doesn't exsist yet.

There is a way around it. I don't remember exactly, but I think it should work if you write something like,

#ifndef TEXTURE_H
#define TEXTURE_H

class Texture;

#include "graphics.h"

class Texture
{
....
Graphics* getGraphics(); one method in my class
};

#endif





[edited by - rotos on July 23, 2002 9:16:19 AM]

Share this post


Link to post
Share on other sites
or if you want to be really sneaky, you don''t have to include the headers in your header files at all (unless you''re using templates). This....


#ifndef _TEXTURE_
#define _TEXTURE_

// Classes used
class Graphics;

class Texture
{
//...
Graphics * m_graphics;
//...
};

#endif // _TEXTURE


Will work fine. Basically it tells the compiler "Hey! There''s this class called Graphics. I''ll tell you about it later!" This, of course, only works if you don''t make any calls to the Graphics class until you get into your CPP file. It also won''t work well if graphics is templated.

Share this post


Link to post
Share on other sites