Circular include problem

hi,
so i just started working on my first ever "big" c++ project
and i've already ran in to a bit of a problem.

i basically have these two classes: MeWindow and NativeWindow

The MeWindow class has a private member variable / pointer of type NativeWindow which gets set in its constructor.
the idea is that it should use this to keep track of the native window.

The NativeWindow takes a pointer to a MeWindow as an argument in its constructor, because the windows properties (title, size, etc) is stored there.

so the problem is that the MeWindow class #includes NativeWindow. But the NativeWindow class then throws an error because MeWindow hasn't been defined for it yet and its trying to take a MeWindow pointer as an argument in its constructor.

Heres my code:

MeWindow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef ME_MEWINDOW
#define ME_MEWINDOW

#include <stdafx.hpp>
#include <NativeWindow.hpp>

namespace Me
{

class MeWindow;
typedef MeWindow* pMeWindow;

class MeWindow
{
private:
	LPCSTR m_pTitle;
	int m_x;
	int m_y;
	bool m_visible;
	NativeWindow* m_pNWind;
public:
	bool Run();
	pMeWindow SetVisible(bool visible);
	pMeWindow SetSize(int x, int y);
	pMeWindow SetTitle(LPCSTR title);
	//bool Add(MeComponent* pComp);
};


}

#define START_ME_WINDOW(TWind)\
	int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPTSTR command, int nShow)\
	{\
		return (new TWind())->Run();\
	}\
	int main()\
	{\
		return (new TWind())->Run();\
	}

#endi 


NativeWindow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef ME_NATIVEWINDOW
#define ME_NATIVEWINDOW

#include <stdafx.hpp>
#include <MeWindow>

#define WIN_CLASS_NAME "meWindow"

namespace Me
{

class NativeWindow
{
private:
	HWND m_hWind;
	WNDCLASSEX m_wndClassEx;
	static LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
public:
	NativeWindow(pMeWindow pWindow);
	bool Show();
};

}
#endi 


How should i go about fixing this? :)
Last edited on
First snippet:
Line 5: delete the recursive include.

Line 9: Include a forward declaration of NativeWindow.
 
class NativeWindow;  //  Forward declaration 


This works as long as all uses in MeWindow of NativeWindow (line 20) are pointers or references.



Last edited on
Topic archived. No new replies allowed.