#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?
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?
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.
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?
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.