This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
My base class (State.h):
#pragma once
#include <Windows.h>
#define WIN32_LEAN_AND_MEAN
#include <gl\GL.h>
#include <gl\GLU.h>
#include "StateManager.h"
class State
{
public:
virtual ~State();
virtual void update(double dt){}
virtual void render(){}
};
The derived class(State_Test.h):
#pragma once
#include "State.h"
class State_Test : public State
{
public:
State_Test();
~State_Test();
};
The errors it gives are in a different class (StateManager.h):
#pragma once
#include "State.h"
#include <map>
#include <string>
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;//error C2976: 'std::map' : too few template arguments | error C2065: 'State' : undeclared identifier | error C2059: syntax error: '>'
std::string m_CurrentState;
void AddState(std::string stateId, State *state);//error C2061: syntax error : identifier 'State'
void ChangeState(std::string stateId);
};
Also there's this warning:
Warning 5 warning C4005: '_malloca' : macro redefinition c:\program files (x86)\microsoft visual studio 12.0\vc\include\crtdbg.h 586 1
Syntactically it looks correct, VS doesn't underline anything, and this code had worked before without error. I'm just wondering if anybody has seen this error before or knows what the cause of it is?
You have circular dependency problem: State.h includes StateManager.h, which includes State.h.
Since StateManager uses only pointer to State, remove this include and forward declare State:
#pragma once
//#include "State.h" -> remove this
#include <map>
#include <string>
class State; //-> add this
class StateManager
{
public:
StateManager();
~StateManager();
std::map<std::string, State *> m_StateMap;
std::string m_CurrentState;
void AddState(std::string stateId, State *state);
void ChangeState(std::string stateId);
};
Related
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 months ago.
My code works normally up until the point that I add #include "CharacterBase.h" to a file called ItemBase.h. I am using #pragma once in all of my files, and I cannot figure out why when I add CharacterBase.h it suddenly is filled with errors. "Itembase" undeclared identifier, is the error.
//Filename is :ItemBase
#pragma once
#include <string>
#include "CharacterBase.h"
class ItemBase
{
}
//Filename is ItemConsumable
#pragma once
#include "ItemBase.h"
class ItemConsumable : public ItemBase
{
}
//File name is CharacterBase.h
#pragma once
#include <string>
#include "ItemBase.h"
#include "ItemConsumable.h"
#include <vector>
class CharacterBase
{
public:
}
You just have a circular dependencie.
In C++, if your file “CharacterBase.h” includes “ItemBase.h” then “ItemBase.h” cannot include “CharacterBase.h”. The only way for ItemBase to use CharacterBase is to forward declare CharacterBase, use pointers or references on CharacterBase in the header and finally include “CharacterBase.h” in “ItemBase.cpp”.
File ItemBase.h
//ItemBase.h
#pragma once
#include <string>
//ItemBase.h
#include "CharacterBase.h"
class ItemBase
{
}
File CharacterBase.h
//CharacterBase.h
#pragma once
#include "ItemBase.h"
class ItemBase{} // Forward declaration
class CharacterBase
{
public:
CharacterBase(ItemBase *base);
ItemBase *itemBase;
}
File CharacterBase.cpp
// CharacterBase.cpp
#include ItemBase.h
CharacterBase:CharacterBase(ItemBase *itemBase){// your stuff}
I am designing a card game program.
In it, the StartStack is a "deck" of cards, StartCard is the card that begins on the bottom of the deck, and AnimalCard and ActionCard are types of cards that can be added to the deck. However, I am getting some very strange errors in Xcode.
Here are my classes:
StartStack.hpp:
#ifndef StartStack_hpp
#define StartStack_hpp
// Imports of other classes
#include "AnimalCard.h"
#include "StartCard.hpp"
class ActionCard;
// Imports of frameworks
#include <deque>
#include <stdio.h>
#include <memory>
class StartStack : public AnimalCard{
private:
deque<shared_ptr<AnimalCard> > sstack;
StartCard scard;
public:
StartStack();
StartStack& operator+= ( std::shared_ptr<ActionCard> );
StartStack& operator-= ( std::shared_ptr<ActionCard> );
std::shared_ptr<StartCard> getStartCard();
char getAnimals(int);
std::shared_ptr<ActionCard> pop_front() { return sstack.pop_front(); }
std::shared_ptr<ActionCard> remove_front();
};
Errors for this class:
Line 29 ("public:") I am getting an "unknown type name StartCard" error
Line 36 ("std::shared_ptr pop_front() { return sstack.pop_front();") I am getting a "use of undeclared identifier StartCard error."
StartCard.hpp:
#ifndef StartCard_hpp
#define StartCard_hpp
// Imports of other classes
#include "NoSplit.hpp"
// Imports of frameworks
#include <stdio.h>
#include <deque>
class StartCard : public NoSplit{
public:
StartCard();
char getAnimals(int index){return c;};
};
ActionCard.h:
// Imports of other classes
#include "QueryResult.hpp"
#include "Player.hpp"
#include "NoSplit.hpp"
#include "Table.hpp"
class ActionCard : public NoSplit{
public:
virtual QueryResult query() = 0;
virtual void perform(Table&, Player*, QueryResult ) = 0;
};
Errors for this class:
Line 21 (in between "public:" and "virtual QueryResult query() = 0;" - an empty line) I am getting "unknown type name Table"
AnimalCard.h is a virtual class with a few methods relating to cards, and QueryResult, thus far, is empty with just a constructor that holds nothing. Player.hpp and NoSplit.hpp are compiling just fine as well.
I have looked into related errors in Xcode and haven't come across anything related to the issues I'm having. If anyone could suggest something that might help it would be much appreciated.
So I have searched high and low and maybe I don't know what question I'm trying to ask, and if that's the case please help me figure out what that might be.
I am trying to print out the color of myCar object from the car class, using a function from the myMan object from the man class. I (think) I have the man class as a friend to the car class so I dont know why I cant access the cars private color variable. Can someone tell me why I can't access the cars private color variable? I have to use friend.
I have been trying to do this for more then 20 hours now and I'm at my wits end.I have included all the files I have because I have seen it done other ways (all in one file) and can't seem to reproduce it broken up like this.
Error 1 error C2433: 'man' : 'friend' not permitted on data declarations car.h line 14
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int car.h line 14
Error 3 error C2248: 'car::color' : cannot access private member declared in class 'car' man.cpp line 13
Error 4 error C2061: syntax error : identifier 'car' man.h line 16
Error 5 error C2660: 'man::printCarColor' : function does not take 1 arguments source.cpp line 16
Error 6 error C2061: syntax error : identifier 'car' man.h line 16
car.h
#pragma once
#include <iostream>
#include <string>
#include "man.h"
using namespace std;
class car
{
public:
car();
car(string);
~car();
friend man;
private:
string color;
};
car.cpp
#include "car.h"
car::car()
{
}
car::car(string c){
color = c;
}
car::~car()
{
}
man.h
#pragma once
#include <iostream>
#include <string>
#include "car.h"
using namespace std;
class man
{
public:
string name;
man();
man(string);
void printCarColor(car);
~man();
};
man.cpp
#include "man.h"
man::man()
{
}
man::man(string newname)
{
name = newname;
}
void man::printCarColor(car mycar){
cout << mycar.color;
}
man::~man()
{
}
source.cpp
#include <iostream>
#include <string>
#include "car.h"
#include "man.h"
using namespace std;
int main()
{
car myCar("green");
//cout << myCar.color;
man myMan("Jim");
myMan.name;
myMan.printCarColor(myCar);
system("pause");
return 0;
}
You have a circular include dependency. car.h includes man.h and man.h includes car.h. This cannot work. Remove #include "man.h" from car.h, use friend class man instead of friend man. – n.m.
** hope this isn't a bad way of answer this, credit goes to ^
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;
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.