So I've got a bit of a problem (well two but they're unrelated to each other).
I have two headers that look as follows:
Game.h
#ifndef INIT_GAME_H
#define INIT_GAME_H
#include <deps/deps.h>
#include <handlers/RenderHandler.h>
class Game {
private:
RenderHandler* renderHandler; /* <-- This is line 32 in my actual header */
public:
Game() {};
~Game() {};
int initialise();
void handleEvents();
void update();
void render();
void clean();
}; // class Game
#endif // INIT_GAME_H
RenderHandler.h
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
#include <init/Game.h>
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
But the above gives me an error during compilation:
game.h(32): error C2143: syntax error: missing ';' before '*'
game.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(32): error C2238: unexpected token(s) preceding ';'
As you've probably guessed, I'm trying to store Game's instance in RenderHandler and vice versa. I'm probably doing it completely the wrong way but I can't figure why it's not working.
Also, all ; are in their right places prior to line 32 in my header file.
EDIT:
after doing the suggested forward declaration, I get the following error (now in RenderHandler.cpp file).
Error: pointer to incomplete class type is not allowed
This is what my code file looks like
RenderHandler.cpp
#include <handlers/RenderHandler.cpp>
void RenderHandler::initialise(Game* game) {
this->game = game;
}
void RenderHandler::render() {
glfwSwapBuffers(game->getPrimaryWindow());
}
Use forward declaration :
#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H
// FW declaration of Game
class Game;
class RenderHandler {
private:
Game* game;
public:
RenderHandler() {};
~RenderHandler() {};
void initialise(Game* game);
void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
Related
I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.
Game.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
#include "TitleScreen.h"
class Game
{
protected:
sf::RenderWindow window;
GameScreen* CurrentGameScreen;
TitleScreen Title;
public:
Game(void);
~Game(void);
sf::RenderWindow getWindow(void);
void Run();
void Close();
};
GameScreen.h:
#pragma once
#include "Game.h"
class GameScreen
{
public:
GameScreen(void);
~GameScreen(void);
virtual void LoadAllResources() {};
virtual void DrawScreen(Game* game) {};
virtual void Update(Game* game) {};
};
TitleScreen.h:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameScreen.h"
class TitleScreen : public virtual GameScreen
{
private:
sf::Texture title_screen;
sf::Sprite titleScreen;
sf::Font font;
sf::Text menuExit;
public:
TitleScreen(void);
~TitleScreen(void);
void LoadAllResources();
void DrawScreen(Game* game);
void Update(Game* game);
};
Then there's the main file:
#include "Game.h"
int main()
{
Game game;
game.Run();
//sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
//GameScreen* currentScreen;
//TitleScreen titleScreen;
//currentScreen = &titleScreen;
//while (window.isOpen())
//{
// currentScreen->Update(&window);
// currentScreen->DrawScreen(&window);
//}
return 0;
}
GameScreen.h and TitleScreen.h raise a handful of C2061. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h.
TitleScreen.h is giving me error C2504: 'GameScreen' : base class undefined.
Game.h: on lines 12 and 13, give C2143: syntax error : missing ';' before '*', although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.
If I remove the #include statement from GameScreen.h and substitute it with a forward declaration class Game; (which I guess breaks the circular dependency?) most of the above is solved, but TitleScreen.cpp throws a set of C2027, C2227 and C2228 (undefined type, left of -> and left of .) every time I try to access a Game object. IntelliSense points out that a pointer to an incomplete class is not allowed.
I got it working before introducing the Game class - DrawScreen() and Update() would take as argument a pointer to window (sf::RenderWindow* window). There's part of the old code left in main.cpp.
In the GameScreen.h you should declare the Game class instead of including its whole header file, so this:
class Game;
instead of:
#include "Game.h"
So I have been having this extremely frustrating problem lately with Visual C++ 2012. Up until a few hours ago, I was writing code just fine and everything was working as intended, until I decided to optimize some things and deleted a few classes. I fixed all of the errors that were popping up because of that, e.g. false includes, etc. Unfortunately, after this the VS compiler went crazy. It started giving me errors such as:
Error 14 error C2653: 'Class' : is not a class or namespace name
or even
Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'
I've checked multiple times, and everything is in it's right place: all headers included, all symbols placed where they should be.
As far as I understand, the problem is not with my code but with the compiler itself... Visual Studio can be really annoying at times, I guess. Anyway, I would really be grateful if someone could help me out on this one.
(By the way, disabling precompiled headers did not work)
Relevant parts of code:
Error 14:
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error
Error 5:
class GameScreen : public BaseScreen
{
public:
...
private:
...
}; // This line causes the error
Error 4:
private:
std::vector<BaseEntity*> _EntityList; // This line causes the error
Whole PlayerEntity.h file:
#ifndef PENTITY_H
#define PENTITY_H
#include "BaseEntity.h"
class PlayerEntity : public BaseEntity
{
public:
PlayerEntity(void);
PlayerEntity(float, float);
virtual ~PlayerEntity(void);
void render(sf::RenderWindow&);
void update();
private:
void init();
};
#endif
Whole GameScreen.h file:
#ifndef GSCREEN_H
#define GSCREEN_H
#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"
class GameScreen : public BaseScreen
{
public:
GameScreen(sf::Vector2u&);
virtual ~GameScreen(void);
void start();
void stop();
void render(sf::RenderWindow&);
void update(void);
void addEntity(BaseEntity*);
void destoryEntity(int id);
private:
std::vector<BaseEntity*> _EntityList;
sf::Vector2u _ScreenDimensions;
};
#endif
Whole BaseEntity.h file:
#ifndef BSENTITY_H
#define BSENTITY_H
#include "Input.h"
#include <SFML/Graphics.hpp>
class BaseEntity
{
public:
BaseEntity(void);
virtual ~BaseEntity(void);
sf::Vector2f position;
virtual void update(void);
virtual void render(sf::RenderWindow&);
void compare(BaseEntity*);
protected:
sf::Texture *_EntityTexture;
sf::Sprite _EntitySprite;
bool _isAlive;
int _id;
virtual void init();
};
#endif
Whole Input.h file:
#ifndef INPUT_H
#define INPUT_H
#include "ScreenSystem.h"
#include <SFML/Window.hpp>
class Input
{
public:
Input(void);
Input(sf::RenderWindow*);
virtual ~Input(void);
static bool keyPressed(int);
static bool keyReleased(int);
static bool mouseHeld(int);
static bool mouseReleased(int);
private:
static sf::RenderWindow *_Window;
};
#endif
Whole ScreenSystem.h file:
#ifndef GHANDLER_H
#define GHANDLER_H
#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>
class ScreenSystem
{
public:
ScreenSystem(void);
ScreenSystem(sf::RenderWindow*);
virtual ~ScreenSystem(void);
BaseScreen *getCurrentScreen(void);
void setScreen(int);
private:
int _currentScreenID;
std::vector<BaseScreen*> _Screens;
sf::RenderWindow *_Window;
};
#endif
You have a circular dependency in your headers. BaseEntity.h includes Input.h, which includes ScreenSystem.h, which includes GameScreen.h, which in turn re-includes BaseEntity.h. This leads to class names appearing before they are declared, causing compilation failure.
To avoid this, do not include headers unnecessarily. For example, do not include Input.h from BaseEntity.h, since it's not needed at all; and do not include BaseScreen.h from ScreenSystem.h since only a declaration class BaseScreen; is needed, not the complete class definition.
Also, check that you do not have duplicate header guards. Some of them do not match the header name (e.g. GHANDLER_H for ScreenSystem.h), which makes me think that they may have been accidentally copied from other headers. Finally, don't use reserved names like _EntitySprite for your own symbols; for simplicity, avoid leading or double underscores.
Did you copy the error messages into your question or did you retype them? Because error 14 has 'Class' with a capital C which is almost certainly not right.
Also, you should use as few include directives in your header files as possible. For example, GameScreen doesn't use PlayerEntity, so you can remove that include and BaseEntity is only used via pointer so you can replace
#include "BaseEntity.h"
with a forward declaration
class BaseEntity;
My header for my class is
#ifndef _CENGINE_H
#define _CENGINE_H
#include "SFML\Graphics.hpp"
#include "CTextureManager.h"
#include "CTile.h"
class CEngine
{
private:
//Create instance of CTextureManager
CTextureManager textureManager;
//Load textures
void LoadTextures();
//New tile
CTile* testTile;
bool Running; //Is running?
sf::RenderWindow* window; //Create render window
public:
CEngine(); //Constructor
int Execute(); //Execute
bool OnInit(); //On intialization
void GameLoop(); //Main game loop
void Render(); //Render function
void Update(); //Update
};
#endif
Now the 3 errors it's giving me with this are:
cengine.h(8): error C2236: unexpected 'class' 'CEngine'. Did you forget a ';'?
cengine.h(8): error C2143: syntax error : missing ';' before '{'
cengine.h(8): error C2447: '{' : missing function header (old-style formal list?)
I know the errors are obvious but I can't see a problem with the class. I'm probably being really stupid because I'm tired.
It seems like a circular include issue. Do CTextureManager.h or CTile.h include each other or CEngine.h?
To solve this, use forward declarations where possible. For example, your class doesn't need to include CTile.h - it can look like:
#ifndef CENGINE_H
#define CENGINE_H
#include "SFML\Graphics.hpp"
#include "CTextureManager.h"
class CTile; //forward declaration instead of include
class CEngine
{
private:
//Create instance of CTextureManager
CTextureManager textureManager;
//Load textures
void LoadTextures();
//New tile
CTile* testTile;
bool Running; //Is running?
sf::RenderWindow* window; //Create render window
public:
CEngine(); //Constructor
int Execute(); //Execute
bool OnInit(); //On intialization
void GameLoop(); //Main game loop
void Render(); //Render function
void Update(); //Update
};
#endif
Similar for the other 2 headers.
Also, _CENGINE_H is not a valid identifier - note how I renamed it to CENGINE_H.
In my program I got two header files named "invader.h" and "game.h". In game.h I include invader.h, and because I wanto to pass a pointer of the current game instance to an invader instance. I also include game.h in invader.h, but I got compile error. If I remove game.h from invader.h, it works fine. I already added include guard in each header files. Based on what I have found so far, I added a forward declaration of game class in invader.h, because what I need, is a pointer to the game instance in invader.h. But when I want to call a function of game in invader.cpp, it says pointer to incomplete class type is not allowed. What should I do to solve this problem?
Game.h
#ifndef GAME_H
#define GAME_H
#include "Tank.h"
#include "Invader.h"
#include "Block.h"
#include "Bullet.h"
class Game
{
private:
Tank tank;
Invader invaders[11][5];
Block blocks[4];
bool logicRequiredThisLoop = false;
public:
Game();
void initEntities();
Tank* getTank(){return &tank;};
Invader* getInvaders(){return &invaders[0][0];};
Block* getBlocks(){return &blocks[0];};
void updateLogic();
};
#endif
Invader.h
#ifndef INVADER_H
#define INVADER_H
#include "Entity.h"
class Game; //forward declaration of class Game
class Invader: public Entity
{
private:
Game* game;
public:
Invader(){};
Invader(Game*,char*,int,int,int,int,int,int);
void move(long delta);
void doLogic();
};
#endif
Invader.cpp
#include "Invader.h"
Invader::Invader(Game* game,char* sprite,int x,int y,int dx,int dy,int width,int height):Entity(sprite,x,y,dx,dy,width,height)
{
this->game = game;
}
void Invader::move(long delta)
{
if ((dx<0)&&(x<=10))
{
game->updateLogic();
}
if ((dx>0)&&(x>=390))
{
dx = -dx;
y -= dy;
}
x+=dx;
}
in Invader.cpp when I try to call updateLogic() which is a member function of Game class, an error occurs saying the pointer to an incomplete class is not allowed
Actually to be simple the most basic thing I want to know here is: in my code Game class has an invader type member variable, so how can I call member functions of Game class in invader class?li,e I said if I include Invader.h in Game.h and include Gameh.h in Invader.h an compile error occurs.
this is what in get when I include Game.h in Invader.h:
1>ClCompile:
1> Invader.cpp
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C2146: syntax error : missing ';' before identifier 'invaders'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2143: syntax error : missing ';' before '*'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): warning C4183: 'getInvaders': missing return type; assumed to be a member function returning 'int'
1>c:\users\tony\documents\info3220\spaceinvader\spaceinvader\basicwogl\game.h(21): error C2065: 'invaders' : undeclared identifier
what should i do to solve this problem?
As a First understand what an Incomplete type means:
What leads to incomplete types?
If you cannot use Forward declarations without the type being Incomplete type then you shoud re-visit your design because something is wrong there.
You will need to provide the source code if you need a more detailed answer.
EDIT:
You need to include Game.h in Invader.cpp.
//Invader.cpp
#include "Invader.h"
#include "Game.h"
I will explain this in reference to two files i have at my local box right now.
server.h and worker.h
server.h:
#ifndef SERVER_H_
#define SERVER_H_
typedef struct _conf conf;
typedef struct _worker worker;
typedef struct _logger logger;
typedef struct _server server;
struct _server {
/* config */
conf *cfg;
/* socket */
int fd;
struct event_base *base;
struct event *signal;
/* workers */
worker **w;
/* log */
logger *log;
};
...
...
#endif /* SERVER_H_ */
worker.h
#ifndef WORKER_H_
#define WORKER_H_
#include <pthread.h>
typedef struct _server server;
typedef struct _worker worker;
struct _worker {
pthread_t t;
struct event_base *base;
struct evhttp *http;
server *s;
};
...
...
#endif /* WORKER_H_ */
As you can see both server and worker structs make a reference to each other which is solved by forward declarations at the top of .h files: e.g.
typedef struct _worker worker;
at top of server.h is sufficient for it to make a reference to worker struct.
You may want to check complete files for further reference here: https://github.com/abhinavsingh/pulsar/tree/master/include
Hope that helps.
I'm receiving the forbids declaration without type error in a Qt application I'm working on. The problem is that I have included the header file which declares the class. It should be defined as a type as far as I can tell. I tried forward declaration as well, but I'd like to use the methods of the class in this file and so I need the whole header file.
code:
Shapes.h
#ifndef SHAPES_H
#define SHAPES_H
#include "Colors.h"
#include <QPoint>
#include "glwidget.h"
//class GLWidget;
class Shape
{
public:
virtual void draw();
};
class Rectangle : public Shape
{
public:
Rectangle(GLWidget *w, QPoint tl, QPoint br);
virtual void draw(){
// top horizontal
for(int i = topLeft.x(); i < btmRight.x(); i++){
glWidget->setPixel(i,topLeft.y(), color);
}
}
private:
QPoint topLeft,btmRight;
GLWidget *glWidget; /**** This is the Error line ****/
RGBColor color;
};
#endif // SHAPES_H
glwidget.h:
#ifndef AGLWIDGET_H
#define AGLWIDGET_H
#include <QGLWidget>
#include "Colors.h"
#include "Shapes.h"
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
~GLWidget();
QSize minimumSizeHint() const;
QSize sizeHint() const;
void setPixel(int x, int y, RGBColor c);
public slots:
void setColor(RGBColor c);
void setDrawRectangle();
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
QPoint lastPos;
QVector<QPoint> drawPoints;
RGBColor paintColor;
int drawmode;
Shape *currentShape;
};
#endif
I don't see why I can't use my GLWidget in Shapes.h. The error is in Shapes.h on the line where I declare GLWidget *glWidget;
If I use a forward declaration ie. class GLWidget; this error goes away but then I can't actually use the GLWidget methods as in Rectangle.draw()
Anyone have an idea why the compiler wouldn't see GLWidget as a type in Shapes.h?
Exact errors:
Shapes.h:20: error: expected ')' before '*' token
Shapes.h:31: error: ISO C++ forbids declaration of 'GLWidget' with no type
Shapes.h:31: error: expected ';' before '*' token
Shapes.h: In member function 'virtual void Rectangle::draw()':
Shapes.h:25: error: 'glWidget' undeclared (first use this function)
Shapes.h:25: error: (Each undeclared identifier is reported only once for each function it appears in.)
You have a circular dependency between your header files -- Shapes.h includes glwidget.h, which includes Shapes.h again. So, the second time the compiler tries to include glwidget.h, the include guard AGLWIDGET_H has been defined, so it doesn't include it again, and then the code in Shapes.h tries to use types that ought to have been declared but are not.
To fix it, you need to remove one of the dependencies and use forward declarations instead. Since glwidget.h doesn't actually use the Shape class beyond declaring a pointer member variable, you should remove its inclusion of Shapes.h:
glwidget.h:
#ifndef AGLWIDGET_H
#define AGLWIDGET_H
// do NOT include Shapes.h
class Shape; // forward declaration
class GLWidget : public QGLWidget
{
...
Shape *currentShape;
};
#endif
Shapes.h is the same as before:
#ifndef SHAPES_H
#define SHAPES_H
#include "glwidget.h"
...
#endif
The reason is the include order - you use a circular inclusion and only the include guards prevent you from looping the compiler.
You can resolve this in your particular case by forward-declaring the shape class in glWidget.h and removing the include for Shapes.h:
//#include "Shapes.h"
class Shape;