#include problem

Started by
3 comments, last by Vampir 21 years, 8 months ago
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;
Advertisement
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]
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.
-Warden
i will try it !
Thanks! It works!

This topic is closed to new replies.

Advertisement