Skip to main content
gamedev.net
🔒 Locked

#include problem

Started by Vampir 3 replies 1.1k views
Original Post
Vampir
Vampir
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;
rotos
rotos
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]
Warden
Warden
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
Vampir
Vampir
i will try it !
Vampir
Vampir
Thanks! It works!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.