How to deal with #include in a program that needs to "circular" correctly?

I am sorry that I cannot find a better term to describe in the title. Anyway, here's my problem:

ControllerInterface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CONTROLLERINTERFACE_H_INCLUDED
#define CONTROLLERINTERFACE_H_INCLUDED
#ifdef _WIN32
#pragma once
#endif
#endif
#include "ModelInterface.h"
#include "ViewInterface.h"

class ControllerInterface{
	ViewInterface* view;
	ModelInterface* model;
public:	
	ControllerInterface();
	ControllerInterface(ModelInterface* m);
};


ModelInterface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MODELINTERFACE_H_INCLUDED
#define MODELINTERFACE_H_INCLUDED
#ifdef _WIN32
#pragma once
#endif
#endif
#include "GameLoop.h"

class ModelInterface{
	GameLoop* desiredGameLoop;
	LoopElements* loopElements;
public:
	ModelInterface();
	virtual bool initSDL();
	virtual void gameLoop();
	virtual void setGameLoop(GameLoop* gl);
	virtual void setLoopElements(LoopElements* le);
};


ViewInterface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef VIEWINTERFACE_H_INCLUDED
#define VIEWINTERFACE_H_INCLUDED
#ifdef _WIN32
#pragma once
#endif
#endif

#include "SDL.h"
#include "ControllerInterface.h"
#include "ModelInterface.h"
class ViewInterface{
	SDL_Surface* screen;
	ControllerInterface* controller;
	ModelInterface* model;

public:
	ViewInterface();
	ViewInterface(ControllerInterface* c, ModelInterface* m);
	virtual SDL_Surface* initScreen();
	virtual SDL_Surface* initScreen(int width, int height, int color, int parameter);
	virtual SDL_Surface* getScreen();
};


Errors:
1
2
3
4
5
1>c:\documents and settings\unknownthreat\my documents\visual studio 2008\projects\sighsdl\sighsdl\controllerinterface.h(11) : error C2143: syntax error : missing ';' before '*'

1>c:\documents and settings\unknownthreat\my documents\visual studio 2008\projects\sighsdl\sighsdl\controllerinterface.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\documents and settings\unknownthreat\my documents\visual studio 2008\projects\sighsdl\sighsdl\controllerinterface.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


After googling about this, I am pretty confident that the problem lies in "#include". I am trying to create a "Model View Controller" design pattern in my C++ program. The thing is I need the data type of another class in order to store the pointer. (ex. ViewInterface* view in ControllerInterface class) However, doing so required me to #include ViewInterface.h, and ViewInterface.h itself also include ControllerInterface.h and ModelInterface.h.

I think this is the problem. I need to be able to linked these classes in a circle, without them fighting each other.

Nevertheless, there is one weird situation where all the errors will be gone, if I remove ViewInterface* view in ControllerInterface class (and I still have the #include "ViewInterface.h" in the code), but I need this ViewInterface*.

Anybody has a good suggestion for this? Or is there something I am not aware of?

Thank you in advance.
You can use class forward declarations instead of including headers
I see. Thanks, Bazzy.

i have a little doubt on this Bazzy..!! i think forward declaration will not remove the error..

just a thought.. lets see if untitled returns..
They should, that is the reason for them to exist
The errors are no longer presented.

Why do you think the errors will still exist, writetonsharma? Are there some sort of concerns that should be addressed?

One more question:
If I write many header files holding my own classes, where should be the ideal place to #include them? Only in their counterpart .cpp files? (ex. myFile.h ---> myFile.cpp) Or should I #include all of my classes header files in stdafx.h?

Currently I #include all of my header files in stdafx.h, and things are fine. However, I am a little afraid because I don't have that much experience in C++. What I am afraid is that I create a new header file called class Component.h and I simply use forward declaration without #include Component.h anywhere in the project. I am not sure where this will lead to. What should be correctly done in this case?
Last edited on
You will want to include your class's .h file in the class's .cpp file, and include the .h wherever you will need the class.
Bazzy:

i dont remember clearly the exact case, but a couple of years back i was designing my classes something of this kind only and my error wont go.. it was something of the same kind what untitled is trying to do thats why i said this. but as the errors are no more there i might be doing something else that time. but one this is sure there was circular referencing between the classes and finally i declared both the classes in the same .h file with a forward declaration of second class on top.

untitled

it will be better if you #include them in respective .cpp files. but there is no rule for including .h files. depends on case to case.
as "untitled" is working on windows, might be it wont compile on linux.
will update you guys on this in sometime.
P.S Those header guards are most unusual.
closed account (z05DSL3A)
The guards are wrong, one of the #endifs is in the wrong place:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CONTROLLERINTERFACE_H_INCLUDED
#define CONTROLLERINTERFACE_H_INCLUDED
#ifdef _WIN32
#pragma once
#endif
// #endif  <--- wrong place

#include "ModelInterface.h"
#include "ViewInterface.h"

class ControllerInterface{
	ViewInterface* view;
	ModelInterface* model;
public:	
	ControllerInterface();
	ControllerInterface(ModelInterface* m);
};
#endif  // 


And as Bazzy said, use forward declarations instead of including, would be better in this case.
Last edited on
Thanks for the replies, people, and sorry for the late reply.

Just two more questions though. Currently I have my header guards like this:

1
2
3
4
5
6
#ifndef CONTROLLERINTERFACE_H_INCLUDED
#define CONTROLLERINTERFACE_H_INCLUDED
#ifdef _WIN32
#pragma once
#endif
#endif  


Grey Wolf said one of the #endif is in the wrong place. I've fixed that. But I am just curious though: my program was also running fine with the #endif in the wrong place. What would be the side effect if I put this #endif in the wrong place?

Second question: for Windows, I see that I can use #pragma once. But what about other OS? What are the substitutes for the #pragma once?
Last edited on
You can always see the output of the preprocessor yourself. In Visual Studio, it's an option under Configuration->C/C++->Preprocessor. It's useful to have a look at least once to get an idea what the poor old compiler has to contend with.

#pragma was introduced then discouraged in GNU's compiler years ago. It's use is discouraged because #pragma once is unreliable. It comes down to the question, "Can the compiler be always sure this is the same file as one it's seen before?" The answer is no.

Window's organisation of and view of files is less sophisticated than Unix, so it's less of a problem.
Last edited on
Topic archived. No new replies allowed.