Problem with multiple #include's

Started by
9 comments, last by Xeno 23 years, 6 months ago
ok , lets say i got two C++ header files : Display.h Surface.h and in these two headers im defining class''s and in each of them im #include the other header file , like that: Display.h:

#ifndef __DISPLAY_H
#define __DISPLAY_H

#include ... // all the directx stuff
#include ... // all the directx stuff
#include ... // all the directx stuff

#include "Surface.h"

class CDisplay {

.... // some code here
....
....
};

typedef CDisplay *LPCDisplay;

#endif
 
Surface.h:

#ifndef __SURFACE_H
#define __SURFACE_H

#include ... // all the directx stuff
#include ... // all the directx stuff
#include ... // all the directx stuff

#include "Display.h"

class CSurface {

.... // some code here
....
....
};

typedef CSurface *LPCSurface;

#endif
 
now , when im trying to compile something like it , im getting lots of error , like redefintion error etc.... here is some of them: :\gearcade\cgsurface.h(31) : error C2535: ''long __thiscall CGSurface::CreateSurface(void)'' : member function already defined or declared c:\gearcade\cgsurface.h(30) : see declaration of ''CreateSurface'' c:\gearcade\cgsurface.h(33) : error C2061: syntax error : identifier ''LPCGDisplay'' why its happening ? ************************* "Everything you know is wrong" - Bono *************************

- Goblineye Entertainment

------------------------------- Goblineye Entertainment------------------------------

Advertisement
anyone?
tnx

*************************
"Everything you know is wrong" - Bono
*************************


- Goblineye Entertainment

------------------------------- Goblineye Entertainment------------------------------

Not good... including those .h files in one another... Avoid if possible.

But if you really need to do something like that, (say, if CDisplay has a pointer to a CSurface and a CSurface has a pointer to a CDisplay), you should declare the classes like this:
                    #ifndef __DISPLAY_H#define __DISPLAY_H#include ... // all the directx stuffclass CSurface;//says that there's a class called Surface, //but we're not going to implement it here...class CDisplay {.... // some code here........CSurface *m_psurf;};typedef CDisplay *LPCDisplay;#endif                    

And you have to use pointers. CDisplay can't have a CDisplay object if CDisplay has a CSurface object... of various reasons... For example, the compiler needs to know the size of the classes, and when calculating the size of a CDisplay containing a CSurface, it needs the size of a CSurface, which in turn has a CDisplay object, which in turn has a CSurface object.... (repeat until bored )

//Ksero

[EDIT] : Made the post look proper (a line was too long)

Edited by - Ksero on October 8, 2000 6:08:28 AM
nono , u dont understand thats not what im trying to do, i will post the real code now:

thats how the CGDisplay looks like:
        #ifndef __CGDISPLAY_H#define __CGDISPLAY_H#include "General.h"#include "CGSurface.h"  /* THATS THE PROBLEM */class CGDisplay {		friend class CGSurface;	friend class CG4pPolygon;	private:		// ddraw stuff		LPDIRECTDRAW7 lp_ddraw_obj;		LPDIRECTDRAWSURFACE7 lp_primary;		LPDIRECTDRAWSURFACE7 lp_back;		DDSCAPS2 ddscaps2;		DDSURFACEDESC2 ddsd2;		DDBLTFX ddbltfx;		// d3d stuff		LPDIRECT3D7 lp_d3d_obj;		LPDIRECT3DDEVICE7 lp_d3d_device;		D3DVIEWPORT7 d3d_viewport;		// screen size & bpp		int X;		int Y;		int bpp;		LPDIRECTDRAW7 GetDDrawObj(void);		LPDIRECTDRAWSURFACE7 GetBackBuffer(void);		LPDIRECT3D7 GetD3dObj(void);		LPDIRECT3DDEVICE7 GetD3dDevice(void);	public:		CGDisplay();		~CGDisplay();		HRESULT ClearD3dDevice(D3DCOLOR color);		HRESULT BeginScene(void);		HRESULT EndScene(void);		HRESULT InitDisplay(int SWidth,int SHeight,int BPP,HWND hWnd);		HRESULT Clear(DWORD color);		HRESULT WriteText(int x,int y,COLORREF tcolor,COLORREF bgcolor,char *text);		HRESULT Flip(void);		HRESULT Restore(void);		void EnableAlphaBlending(void);		void DisableAlphaBlending(void);		void EnableBLinearFiltering(void);		void DisableBLinearFiltering(void);		void EnableTextureColorKey(void);		void DisableTextureColorKey(void);		int GetScreenWidth(void);		int GetScreenHeight(void);		int GetBpp(void);		HRESULT SetRenderTexture(LPCGSurface lp_texture);};typedef CGDisplay *LPCGDisplay;#endif[/source]and thats how the CGSurface looks like:[source]#ifndef __CGSURFACE_H#define __CGSURFACE_H#include "General.h"#include "CGDisplay.h"#include "ddutil.h"class CGSurface {	friend class CG4pPolygon;	private:		LPDIRECTDRAWSURFACE7 lp_surface;		DDSURFACEDESC2 ddsd2;		D3DDEVICEDESC7 device_desc;		DDBLTFX ddbltfx;		HBITMAP hbm;		int surface_width;		int surface_height;		char *bitmap_name;		LPDIRECTDRAWSURFACE7 GetSurface(void);	public:		CGSurface();		~CGSurface();		BOOL b_d3d_tex;		HRESULT CreateSurface(LPCGDisplay lp_display,int width,int height,bool video_mem);		HRESULT CreateSurface(LPCGDisplay lp_display,int width,int height);		HRESULT LoadBitmap(char *filename);		HRESULT BltFast(LPCGDisplay lp_display,int x,int y,bool colorkey);		HRESULT SetTransColorKey(COLORREF color);		HRESULT Clear(DWORD color);		HRESULT Restore(void);		int GetHeight(void);		int GetWidth(void);};typedef CGSurface *LPCGSurface;#endif        


u see when im adding the ' #include "CGSurface.h" ' , then im geting the errors , im not trying to define a pointer to CGSurface in the CGDisplay class , im just want CGDisplay class to recornize the CGSurface class


*************************
"Everything you know is wrong" - Bono
*************************


- Goblineye Entertainment

Edited by - xeno on October 8, 2000 6:31:32 AM

------------------------------- Goblineye Entertainment------------------------------

anyone ?, please?
tnx

*************************
"Everything you know is wrong" - Bono
*************************


- Goblineye Entertainment

------------------------------- Goblineye Entertainment------------------------------

The only way I can see getting you to realize what you're doing wrong is to show you the simplest example.

First I'll try to explain. You're declaring the header file in the same header you're declaring...LOL(I laugh because that made no sence ).

Example:

            //--- the name of this file is header.h ---// you're declaring the name of this header, that's totaly ilegal#include "header.h"              


If you must do it the way you're doing it, make a seperate header to place "typedef CGDisplay *LPCGDisplay;" and "typedef CGSurface *LPCGSurface;" in. Remove the headers that are included in the way I explained above and replace then with the new header containing your LP typedef variables.

Good luck.

Edited by - WhatEver on October 8, 2000 11:21:33 AM

Edited by - WhatEver on October 8, 2000 11:32:19 AM
WhatEver''s right think of the source code after it''s been through the preproccessor

Display.h:

// stuff
#include "Surface.h"
// which expands to
// stuff
#include "Display.h"
// which expands to
// stuff
#include "Surface.h"
"Don't make me come down there..." -GOD
actually, WhatEver and Ecco, that''s not right... the preprocessor directive #ifndef is preventing multiple inclusion. once the header has been included once, it doesn''t expand if it''s included more, just ends the chain there.. (if that made sense)
i believe Ksero''s suggestion is correct, though.. lemme try it and see...

------------------------
IUnknown *pUnkOuter

"Just do your best and
don't worry"
--Morrissey

"Try the best you can
try the best you can
the best you can is good enough"
--Radiohead

"Are you looking for new ways
to do better than your worst"
--Nick Drake
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
hmm.... sorry for the late , but what Ksero saied was the solution, i had to declare this line:
class CGSurface;
in the CGDisplay.h

and this one:
class CGDisplay;
in the CGSurface.h

then everything goes perfect

tnx anyway


*************************
"Everything you know is wrong" - Bono
*************************


- Goblineye Entertainment

Edited by - xeno on October 8, 2000 3:11:08 PM

------------------------------- Goblineye Entertainment------------------------------

Cool, ya learn something new every day :D. I didn''t know you could start a class declaration and then declare it somewhere else.

Couldn''t you also do this then?

    extern class CSurface;    

This topic is closed to new replies.

Advertisement