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.
Related
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
So, I'm having what is likely a pretty simple problem, but I can't seem to figure out what is causing it.
I have a C++ class called "Game" consisting of a class declaration in Game.h
and a source definition in Game.cpp.
I have included "Game.h" in my "Game.cpp", but for some reason Visual Studio doesn't seem to recognize the class declarations within Game.h.
I'd appreciate any help I could get trying to figure out why this is the case, and why I am getting the following errors:
'MyCharacter' : undeclared identifier and 'HandleKeyPressed' : identifier not found.
Game.h:
------------------------------------------------------------------------------------------------------------------
#pragma once
#ifndef Game_Header
#define Game_Header
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Character.h"
#include "FloorPlatform.h"
class Game
{
public:
Game();
~Game();
int Run();
void HandleKeyPressed(sf::Keyboard::Key);
Character MyCharacter;
};
#endif // !Game_Header
Abridged Game.cpp
#include "Game.h"
Game::Game()
{
}
Game::~Game()
{
}
int Run(){
sf::RenderWindow MainGameWindow(sf::VideoMode::getDesktopMode(), "A Test Game");
//Start Game Loop
while (MainGameWindow.isOpen()){
while (MainGameWindow.pollEvent(event)){
//Handle some other events here...
if (event.type == sf::Event::KeyPressed){
HandleKeyPressed(event.key.code);
}
}
MainGameWindow.clear(sf::Color::White);
MyCharacter.Instance.Circle.setPosition(MyCharacter.PlayerLocation);
MainGameWindow.draw(MyCharacter.Instance.Circle);
MainGameWindow.display();
}
return 0;
}
void HandleKeyPressed(sf::Keyboard::Key PressedKey){
switch (PressedKey)
{
case sf::Keyboard::A:
MyCharacter.PlayerLocation.x -= 16;
break;
}
}
Full code can be found here:
http://pastebin.com/x6KhDxgL
Thanks in advance for any help I can get with this.
Try
int Game::Run()
instead of
int Run()
in Game.cpp, the same for HandleKeyPressed, because it's the method of the Game class. Depending on your Character.h you may need to initialize MyCharacter, too.
void HandleKeyPressed(sf::Keyboard::Key PressedKey){
In Game.cpp should be
void Game::HandleKeyPressed(sf::Keyboard::Key PressedKey){
Same for int run()
I'm getting these two errors:
sdlsetup.h(15): error C2143: syntax error : missing ';' before '*'
sdlsetup.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
This is sdlsetup.h line 15:
CText* ctext;
The class is definitely set up properly and included in sdlsetup.h
Why is this?
EDIT: This is sdlsetup.h
#pragma once
#include "stdafx.h"
class SDLsetup
{
public:
SDLsetup();
~SDLsetup();
void Begin();
void End();
SDL_Renderer* GetRenderer();
void EnterGameLoop();
CText* ctext;
private:
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event* mainEvent;
int screenWidth;
int screenHeight;
};
This is Text.h
#pragma once
#include "stdafx.h"
class CText
{
public:
CText();
~CText();
void draw(SDL_Renderer* renderer, std::string str);
SDL_Texture* aGetMessage();
SDL_Rect* aGetMessageRect();
private:
TTF_Font* Sans;
SDL_Color White;
SDL_Surface* surfaceMessage;
SDL_Texture* Message;
SDL_Rect Message_rect;
};
Here the problem is a weird error message from the compiler, because it doesn't know what CText is.
You need to declare CText before you use it:
#pragma once
// Declare the CText class, so the compiler knows it exists
class CText;
class SDLsetup
{
public:
...
CText* ctext;
...
};
You need to add includes for Text.h in sdlsetup.h and also for the relevant SDL headers in both files.
Text.h needs to have SDL.hand SDL_ttf.hincluded and sdlsetup.h needs SDL.hand Text.h
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"
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;