Compiler error C2653: not a class or namespace name - c++

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;

Related

Problem with includes, base class undefined C2504

This is my EngineIncludes.h file:
#include <stdafx.h>
//Engine Includes
namespace Engine
{
//Classes: 23
class BaseObject;
class Console;
class Engine;
class Node;
template <class T> class Transform;
}
#include "core/BaseObject.h"
#include "core/Console.h"
#include "core/Engine.h"
#include "math/Transform.h"
#include "scene/Node.h"
//Global Objects
extern Engine::Console* CONSOLE;
extern Engine::Engine* CORE_ENGINE;
In stdafx.h I have regular stuff like OpenGL, std::map, boost... and I'm building a precompiled header as standard.
This is the Node.h file:
#ifndef _NODE_H
#define _NODE_H
#include <stdafx.h>
#include <EngineIncludes.h>
namespace Engine
{
class Node : public BaseObject
{
public:
Node();
~Node();
void SetParent(Node* parent);
Node* GetParent();
void SetTransform(const Transform<double> &transform);
Transform<double> GetTransform();
Transform<double> GetDerivedTransform();
Transform<double> GetInheritedTransform();
void Update();
private:
Transform<float> transform;
Transform<float> derived;
Node* parent;
};
}
#endif _NODE_H
I get 3 errors here. One C2504 that Engine::BaseObject is not defined. And two C2079 that both Transform transform and Transform use undefined class. Now this is the BaseObject.h:
#ifndef _BASE_OBJECT_H
#define _BASE_OBJECT_H
#include "stdafx.h"
#include "EngineIncludes.h"
namespace Engine
{
class BaseObject
{
public:
BaseObject()
{
id = CORE_ENGINE->GenerateID();
}
~BaseObject()
{
}
long GetID()
{
return id;
}
private:
long id;
};
}
#endif _BASE_OBJECT_H
Now I specifically fully defined BaseObject inside header file with no luck. Still the same error. But if I comment out forward declarations in EngineIncludes.h, the compiler freaks out with
extern Engine::Console* CONSOLE;
It literally ignores all #includes for whatever reason. I've never had an issue like this before. And I tried everything. I even created EngineIncludes.cpp file. I moved the contents of EngineIncludes.h into stdafx.h with no luck. It somehow just ignores #includes. Even if I put #include "BaseObject.h" inside "Node.h" nothing changes. When it compiles Node it has both Transform which is a template class and BaseObject undefined, even though both objects are clearly defined before Node with forward declarations and #includes.
I'm using Visual Studio 2010, but never had an issue like this. I looked into my previous projects and all is written the same fashion and works without problem.

C++ header error C2238 unexpected token ';'

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.

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

C++: Inclusion inside inclusion

The title might not be very clear, it's a bit more complex than that. I searched the web for something like my problem but I did not find anything that could help me.
This is not about infinite looping inclusions, I already put preprocessor directives to avoid that.
I have two classes Monster and Character, respectively declared in their own header files, monster.hpp and character.hpp, and respectively implemented in their own source files, monster.cpp and character.cpp.
The problem now is that both classes need each other to work.
monster.hpp :
#ifndef INCLUDE_MONSTER_HPP
#define INCLUDE_MONSTER_HPP
#include "character.hpp"
class Monster
{
private: //Atributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //MONSTER_HPP_INCLUDED
character.hpp :
#ifndef INCLUDE_CHARACTER_HPP
#define INCLUDE_CHARACTER_HPP
#include "monster.hpp"
class Character
{
private: //Attributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //CHARACTER_HPP_INCLUDED
and the main.cpp :
#include <iostream>
#include "character.hpp"
#include "monster.hpp"
using namespace std;
int main(int argc, char **argv)
{
//Whatever
return 0;
}
And I get this error from the compiler :
In file included from character.hpp:7:0,
from main.cpp:3:
monster.hpp:24:16: error: 'Character' has not been declared
void attackC(Character& id);
(the line and column numbers may be wrong)
From what I understand, when monster.hpp is included into character.hpp, the compiler sees that the class Monster uses the class Character, which is not declared yet right at the moment when monster.hpp is included into character.hpp.
And I don't know how to fix that.
Any ideas ?
The way this works is that the header files of char and monster do not include each other. Instead you forward declare the classes and include the headers within the CPP files.
So basically replace #include "monster.hpp" in the.h with class Monster; and #include "monster.hpp" in the .cpp - and same for the other class.
See this question for more details:
What are forward declarations in C++?
#ifndef INCLUDE_MONSTER_HPP
#define INCLUDE_MONSTER_HPP
#include "character.hpp"
class Character;
class Monster
{
private: //Atributes
public: //Methods
void attackM(Monster& id);
void attackC(Character& id);
};
#endif //MONSTER_HPP_INCLUDED
You need to use a forward declaration. See: http://en.wikipedia.org/wiki/Forward_declaration
Basically, the compiler doesn't know what a "Character" is. You temporarily indicate that it's something you can point to by adding the following stub to Character.hpp:
class Character;
Use predeclaration in *.h:
class Character;
class Monster;
use in *cpp the includes:
#include "character.h"
#include "monster.h"
Or put everything in one *.hpp with predeclaration.

Inheritance Error "Base Class Unidentified"

I have a State class that is fully implemented as outlined below. I also have a PlayState class that inherits the State class, it too is fully implemented. My compile error is "playstate.h(6): error C2504: 'State' : base class undefined"
I have checked their order in Global.h, State.h appears before PlayState.h
CODE:
STATE.H
#pragma once
#include "Global.h"
class State
{
public:
State(void);
virtual ~State(void);
virtual void Input(INPUTDATA* InputData);
virtual void Logic(OBJECT go[], INPUTDATA* InputData);
virtual void Render(OBJECT go[]);
virtual void InitGame(OBJECT go[]);
virtual void LoadGraphics(void);
void Toggle();
bool IsEnabled();
private:
bool isEnabled;
};
PlayState.h
#include "Global.h"
class PlayState : public State
{
private:
#define UPDATESPEED 1000 // milliseconds between each update
// global variables
float camXAngle;
float camYAngle;
float camZoom;
int updatetime;
bool gameover;
float runspeed;
D3DLIGHT9 light;
SPRITE graphics;
SPRITE particleTexture;
MODEL terrain[2];
MODEL sky;
public:
PlayState();
~PlayState();
void Input(INPUTDATA* InputData);
void Logic(OBJECT go[], INPUTDATA* InputData);
void Render(OBJECT go[]);
void InitGame(OBJECT go[]);
void LoadGraphics(void);
};
Thanks
If some *.cpp includes "State.h" without "Global.h" somewhere before it then you will have the error that you've posted.
Because when "State.h" includes "Global.h" then "Global.h" does not include "State.h" (because of #pragma once) but it includes "PlayState.h" so in the end you have "PlayState.h" included before class State is defined.
Just don't make such weird circular inclusions.
If your Global.h already includes State.h and PlayState.h and in the order that State.h is placed before PlayState.h, then there is no reason to get the particular error(for the source code you have posted), unless except you are making some silly typo like missing a caps in State. Please check for typos! or there might be another reason to the problem.
You are building a circular dependency of includes, which should be avoided.
A simple solution might be to not include both includes, State.h and PlayState.h in Global.h.
Just include State.h inside PlayState.h and it should be fine. Global.h wont build up any circular dependencies that way.
#pragma once
#include "Global.h"
class PlayState : public State
What is "State"? That is what the compiler is complaining about.
You can't inherit from a class that has not been fully defined. Looking at the file PlayState.h, nowhere do you specify the State class.
CORRECTED CODE:
#pragma once
#include "State.h"
class PlayState : public State