C++ header error C2238 unexpected token ';' - c++

There is a C2238 error in the PauseMenu header file on the line: Game* game;
It says that ';' is an unexpected token, as well as: C2143 syntax error: missing ';' before '*' on the same line; I have no idea what's wrong in those files, I thought both files are correct.
PauseMenu.h Header File:
#pragma once
#include "EventHandler.h";
#include "MousePressEvent.h";
#include "RectElement.h";
#include "Engine.h";
class PauseMenu :
public EventHandler
{
public:
PauseMenu();
void Show();
void Hide();
void onEvent(Event* event);
void onEvent(MousePressEvent* event);
Game* game;
private:
RectElement* background;
RectElement* resume;
RectElement* options;
RectElement* quit;
bool visible = false;
};
Game.h Header File
#pragma once;
#include "Engine.h";
#include <ctime>;
#include "GameLayer.h";
#include "TextElement.h";
#include "HUDManager.h";
#include <windows.h>;
#include "Collider.h";
#include "CircleCollider.h";
#include "BoxCollider.h";
#include "MouseMoveEvent.h";
#include "CollisionShapeHitEvent.h";
#include "EventHandler.h";
#include "MainMenu.h";
#include "FpsCounter.h";
#include "PauseMenu.h";
#define PI 3.14159265358979323846264338327950288;
class Game :
public EventHandler
{
public:
Game(bool dev);
~Game();
void onEvent(Event* event);
void onEvent(MouseMoveEvent* event);
void onEvent(KeyPressEvent* event);
void onEvent(KeyReleaseEvent* event);
void onEvent(CollisionShapeHitEvent* event);
MainMenu* mainMenu;
PauseMenu* pauseMenu;
private:
Player* player;
Engine* engine;
};

Remove the semicolons after your #includes and #defines. In fact: Remove all semicolons after lines beginning with # if you can't find a reason for them to be there.
And I doubt that you need all those files included in game.h. But you are missing game.h in pausemenu.h or a forward declaration of Game:
class Game;

I guess you have some circular depenency between headers.
To solve it, you can use forward declaration for class Game; in PauseMenu.h Header File before the class PauseMenu.
I guess this way you still maight get other errors but you'll skip the mentioned one

Try to put destructor ~PauseMenu(){}
at the end of class.

Related

Syntax error in c++ default-int [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 4 years ago.
So I just began programming in c++ and I am planning to make a small game as my first project (with the SDL library).
So I have this really small piece of code in a header file that gives me an error that I can not solve.
main.h
#include "Screen.h"
#include "MainGame.h"
Screen* screen = nullptr; //main.h line 8
Screen.h
#pragma once
#include "SDL.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include "GameState.h"
#include "Game.h"
using namespace std;
class Screen
{
public:
Screen(string name, int width, int height, GameState* state);
~Screen();
SDL_Window *window;
SDL_Surface *screen;
SDL_Renderer *renderer;
};
MainGame.h
#pragma once
#include "GameState.h"
#include <stdio.h>
class MainGame :
public GameState
{
public:
MainGame();
~MainGame();
void start();
void update();
void render();
void stop();
};
Game.h
#pragma once
#include "GameState.h"
#include "Screen.h"
#include "SDL.h"
#include "main.h"
class Game
{
public:
GameState* activestate;
Game(GameState state);
~Game();
void changeState(GameState newState);
bool isRunning;
void handleEvents();
void update();
void render();
void stop();
};
GameState.h
#pragma once
class GameState
{
public:
GameState();
~GameState();
virtual void start();
virtual void update();
virtual void stop();
virtual void render();
};
And it gives this errors:
Error C2143 syntax error: missing ';' before '* main.h 8
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int main.h 8
What do these errors mean and how do I solve them?
The error comes from your circular dependency. Some file includes Screen.h. Screen.h includes Game.h. Game.h includes main.h. main.h needs Screen.h but because of pragma once it can't include it. So it doesn't know class Screen. Either remove circular dependency (better solution if possible, here it's possible):
Remove #include "main.h" in Game.h
or use forward declaration:
Write class Screen; in main.h:7

error: expected ')' before '*' token in header

I am making a program where there is a Hero who has a Sword. I have a class for both of those. In the header I get the error: expected ')' before '*' token on the line Sword(Hero* h); in the header of Sword. Here is the compete file (Sword.h):
#ifndef SWORD_H
#define SWORD_H
#include <Hero.h>
class Sword {
public:
Sword(Hero* h);
virtual ~Sword();
};
#endif // SWORD_H
Hero.h is in the same directory as Hero.h, and I'm using Code::Blocks.
I've looked through other posts and couldn't find anything that helped, so any given would be appreciated.
EDIT:
Here is the content of Hero.h:
#ifndef HERO_H
#define HERO_H
#include <string>
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <Sword.h>
#include <Sprite.h>
#include <Window.h>
class Hero : public Sprite {
public:
Hero(Window* w);
void update();
void event(SDL_Event e);
~Hero();
protected:
private:
bool up;
bool right;
bool left;
bool down;
Window* window;
Sword* sword;
};
#endif // HERO_H
You cannot include Sword.h from Hero.h and Hero.h from Sword.h, the inclusion chain has to stop somewhere. You can use a forward declaration to fix it:
//#include <Hero.h> // remove this
class Hero; // forward declaration
class Sword {
public:
Sword(Hero* h);
virtual ~Sword();
};
This works because you don't need the definition of Hero in Sword.h. The compiler only needs to know that Hero is a class.
You can do the same in Hero.h: replace #include <Sword.h> with class Sword;. You can then include the files in the corresponding .cpp files where you need the definitions in order to use the classes.
Rule of thumb: always use forward declaration, unless the whole header needs to be included.
Further reading: When can I use a forward declaration?
It looks like you have a circular dependency. You can fix it with forward declarations:
class Hero; //in Sword.h, before defining Sword
class Sword; //in Hero.h, before defining Hero

Undeclared Identifier-Declarations Present In Header, But Unidentified In Class Source

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()

C++: circular dependency issue

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"

Compiler error C2653: not a class or namespace name

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;