I know there are some topics on forward declaration, but after reading them, they don't seem to match this situation. I usually understand what is going on, a full definition is required to allocate. However, I really think I have a full definition here so I'm confused.
//game.h
#include "wizard.h"
#include "warrior.h"
#include "archer.h"
#include "fighter.h"
#ifndef GAME_H
#define GAME_H
enum movetype{
BOOST,
ATTACK,
SPECIAL,
SHIELD
};
struct move{
enum movetype type;
fighter * source;
fighter * target;
};
class game{
public:
game();
~game();
void execmove(move&);
bool trymove();
private:
bool turn;
fighter* t1 [3];
fighter* t2 [3];
};
#endif
So in game I include all the other classes
In fighter I forward declare warrior:
//fighter.h
#ifndef FIGHTER_H
#define FIGHTER_H
class fighter;
class warrior;
struct link {
fighter * warrior;
fighter * target;
};
class fighter{
private:
public:
fighter();
virtual ~fighter();
void action(int, fighter*);
virtual void special(fighter*);
void attack(fighter*);
void defend();
void tick();
void damage(int,int);
void createLink(warrior *);
void removeLink();
protected:
int hp;
int priority;
int boosts;
int shields;
bool alive;
struct link l;
};
#endif
But then when I try to allocate warrior in game:
//game.cpp
#include <iostream>
#include "game.h"
game::game()
{
t1[0] = new warrior();
t1[1] = new wizard();
t1[2] = new archer();
t2[0] = new warrior();
t2[2] = new archer();
t2[1] = new wizard();
}
game::~game()
{
for(int i = 0; i<3; i++)
{
delete t1[i];
delete t2[i];
}
}
void game::execmove(move& m)
{
}
I get:
game.cpp:9:14: error: allocation of incomplete type 'warrior'
t1[0] = new warrior();
^~~~~~~
./fighter.h:7:7: note: forward declaration of 'warrior'
class warrior;
^
game.cpp:13:14: error: allocation of incomplete type 'warrior'
t2[0] = new warrior();
^~~~~~~
./fighter.h:7:7: note: forward declaration of 'warrior'
class warrior;
It seems like that fact that I forward declared makes the compiler ignore the fact that I also included the full definition which is really odd. I'm wondering if there is something that I am missing because I don't know how to fix it if a forward declaration makes the full definition get ignored.
Bottom line: Why can't I allocate warrior since I included the full warrior.h file in game.h?
Here is warrior.h:
#include "fighter.h"
#ifndef FIGHTER_H
#define FIGHTER_H
class warrior{
public:
warrior();
~warrior();
void special(fighter);
private:
};
#endif
Change warrior.h to: (i.e. correct include guards)
#ifndef WARRIOR_H
#define WARRIOR_H
#include "fighter.h"
class warrior{
public:
warrior();
~warrior();
void special(fighter);
private:
};
#endif
Or better still use #pragma once if supported
You need to use different include guard symbols of different .h files.
"fighter.h" defines FIGHTER_H, so when that #if check is encountered in "warrior.h" the definition of the class warrior is skipped.
The incorrectly named guards were making it not include the warrior file. Thank you #1201ProgramAlarm
Related
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
I'm not exactly sure why I'm getting these errors, I thought it was because I needed a forward declaration, but the problem still persists. I have no clue what is causing this. I put comments where the offending lines are. Looking for an explanation on why this is happening and a resolution. Thanks.
// Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "Weapon.h"
#include "Monster.h"
#include <string>
// Forward declaration
class Race;
class Player
{
public:
Player();
/* Return Type Method Name */
bool IsDead();
std::string GetName();
int GetArmor();
void TakeDamage(int damage);
void CreateClass();
bool Attack(Monster& monster);
void LevelUp();
void Rest();
void ViewStats();
void Victory(int xp, Monster* monster);
void GameOver();
void DisplayHitPoints();
void SetRace();
private:
/* Type Name */
std::string m_Name;
std::string m_ClassName;
int m_Accuracy;
int m_HitPoints;
int m_MaxHitPoints;
int m_ExpPoints;
int m_NextLevelExp;
int m_Level;
int m_Armor;
int m_Gold;
Weapon m_Weapon;
Race m_Race; // problem: error C2079 uses undefined class 'Race'
};
#endif // PLAYER_H
An offending method with a cannot convert from 'Race' to 'int' error
void Player::SetRace()
{
Race race;
m_Race = race.SelectRace(); // problem: error C2440 cannot convert from 'Race' to int
}
Race class definition:
// Race.h
#ifndef RACE_H
#define RACE_H
#include <string>
class Race
{
public:
Race();
/* Return Type Method Name*/
Race SelectRace();
protected:
std::string GetName();
/* Type Name*/
std::string m_Name;
int m_Accuracy;
int m_HitPoints;
};
// ...below here implements multiple derived classes of type race
#endif // RACE_H
Yes, as you've found, a forward declaration is insufficient here. That's because there is a member of type Race in Player.
Include the whole header at the top of player.h:
#include "Weapon.h"
#include "Monster.h"
#include "Race.h"
Then the definition of your Race class will be visible to the definition of Player.
(I have read all the threads posted here and google, I was not able to fix from that)
I am having toruble with a incomplete type error when compiling. The way I am designing the project, the game pointer is unavoidable.
main.cpp
#include "game.h"
// I actually declare game as a global, and hand itself its pointer (had trouble doing it with "this")
Game game;
Game* gamePtr = &game;
game.init(gamePtr);
game.gamePtr->map->test(); // error here, I also tested the basic test in all other parts of code, always incomplete type.
game.h
#include "map.h"
class Map;
class Game {
private:
Map *map;
Game* gamePtr;
public:
void init(Game* ownPtr);
int getTestInt();
};
game.cpp
#include "game.h"
void Game::init(Game* ownPtr) {
gamePtr = ownPtr;
map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}
int Game::getTestInt() {
return 5;
}
map.h
class Game;
class Map {
private:
Game* gamePtr;
public:
int test();
};
map.cpp
#include "map.h"
int Map::test() {
return gamePtr->getTestInt();
}
// returns that class Game is an incomplete type, and cannot figure out how to fix.
Let's go over the errors:
1) In main, this is an error:
game.gamePtr->map->test();
The gamePtr and map are a private members of Game, therefore they cannot be accessed.
2) The Map is missing a constructor that takes a Game* in Game.cpp.
map = new Map(gamePtr);
Here is a full working example that compiles. You have to provide the functions that are missing bodies, such as Map(Game*).
game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
class Map;
class Game {
private:
Map *map;
public:
Game* gamePtr;
void init(Game* ownPtr);
int getTestInt();
};
#endif
game.cpp
#include "game.h"
#include "map.h"
void Game::init(Game* ownPtr) {
gamePtr = ownPtr;
map = new Map(gamePtr); // acts as "parent" to refer back (is needed.)
}
int Game::getTestInt() {
return 5;
}
map.h
#ifndef MAP_H_INCLUDED
#define MAP_H_INCLUDED
class Game;
class Map {
private:
Game* gamePtr;
public:
int test();
Map(Game*);
};
#endif
map.cpp
#include "game.h"
#include "map.h"
int Map::test() {
return gamePtr->getTestInt();
}
main.cpp
#include "game.h"
#include "map.h"
int main()
{
Game game;
Game* gamePtr = &game;
game.init(gamePtr);
game.gamePtr->map->test();
}
After doing this and creating a project in Visual Studio, I do not get any errors building the application.
Note the usage of #include guards, which your original posted code did not have. I also placed the members that were private and moved them to public in the Game class, so that main() can compile successfully.
You need to use forward declaration. Place declaration of Map class before definition of class Game:
game.h
class Map; // this is forward declaration of class Map. Now you may have pointers of that type
class Game {
private:
Map *map;
Game* gamePtr;
public:
void init(Game* ownPtr);
int getTestInt();
};
Every place where you use Map and Game class by creating instance of it or dereferencing pointer to it either through -> or * you have to make that type "complete". It means that main.cpp must include map.h and map.cpp must include game.h directly or indirectly.
Note you forward declare class Game to avoid game.h to be included by map.h, and that is fine and proper, but map.cpp must have game.h included as you dereference pointer to class Game there.
OK so i have a few problems.
1. I need to be able to register the childclass when its created with game for broadcasting.
2. I need to be able to call the child class's method from an array of GameObject Pointers.
3. I need it to be the actual classes, not just copys of them.
In the file systemvars.h i have my global methods and some variables.
#pragma once
//=================================
// include guard
#ifndef _SYSTEMVARS_H_
#define _SYSTEMVARS_H_
//=================================
// forward declared dependencies
class Game;
//=================================
// included dependencies
#include <iostream>
using namespace std;
//global enums
const enum CLASSTYPE{CLASSTYPE_NULL,CLASSTYPE_PLAYER,CLASSTYPE_DUNGION,CLASSTYPE_ENTITY,CLASSTYPE_MAP,CLASSTYPE_MENU};
//Global methods
void setGameRefrence(Game*);
Game* getGameRefrence();
systemvars.cpp: definitions
#include "SystemVars.h"
static Game* curentgame;
void setGameRefrence(Game* mygame)
{
curentgame = mygame;
}
Game* getGameRefrence()
{
return curentgame;
}
In my Game.h: a holder for gameobjects
#pragma once
//=================================
// include guard
#ifndef _GAME_H_
#define _GAME_H_
//=================================
// forward declared dependencies
class GameObject;
//class Player;
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//#include "Player.h"
#include <iostream>
using namespace std;
//=================================
// the actual class
class Game
{
public:
Game(void);
void registerGameObject(GameObject*);
void unregisterGameObject(GameObject*);
void sendMessageToAllObjects(string message,CLASSTYPE recipeint);
~Game(void);
private:
GameObject *gameobjects2[1000];
int numberofobject;
};
#endif
the game.cpp gameclass definition
#include "Game.h"
//#include "Player.h"
Game::Game(void)
{
setGameRefrence(this);
//logHelperMessage(INFO,1,"Registerd game");
numberofobject = 0;
}
void Game::registerGameObject(GameObject* newobj)
{
//logHelperMessage(INFO,1,"Registerd");
newobj->setId(numberofobject);
gameobjects2[numberofobject] = newobj;
numberofobject++;
//gameobjects.push_back(newobj);
}
void Game::unregisterGameObject(GameObject* objtodie)
{
//logHelperMessage(INFO,1,"Unregister");
for(int i = objtodie->getId();i < numberofobject - 1;i++)
{
gameobjects2[i] = gameobjects2[i+1];
gameobjects2[i]->setId(i);
}
gameobjects2[numberofobject-1] = nullptr;
numberofobject--;
}
void Game::sendMessageToAllObjects(string message,CLASSTYPE recipeint)
{
for(int i = 0; i < numberofobject;i++)
{
cout << "Sent the message from game");
//((Player *)gameobjects2[i])->sendMessage(message);
//static_cast<Player*>(gameobjects2[i])->sendMessage(message);
}
}
Game::~Game(void)
{
}
Gameobject.h: the parent of my inner game classes.
#pragma once
//=================================
// include guard
#ifndef _GAMEOBJECT_H_
#define _GAMEOBJECT_H_
//=================================
// forward declared dependencies
enum CLASSTYPE;
//=================================
// included dependencies
#include <iostream>
#include "SystemVars.h"
using namespace std;
//=================================
// the actual class
class GameObject
{
public:
GameObject();
GameObject(CLASSTYPE mytype);
~GameObject(void);
virtual void sendMessage(string data);
virtual CLASSTYPE getMyClassType();
virtual void setMyClassType(CLASSTYPE newrecip);
void setId(int val);
int getId();
protected:
CLASSTYPE _MYCURRENTCLASSTYPE;
int myid;
};
#endif
Gameobject.cpp
#include "GameObject.h"
GameObject::GameObject() : _MYCURRENTCLASSTYPE(CLASSTYPE_NULL)
{
//do not register
}
GameObject::GameObject(CLASSTYPE mytype): _MYCURRENTCLASSTYPE(mytype)
{
//register this object into the gameobject list.
getGameRefrence()->registerGameObject(this);
}
GameObject::~GameObject(void)
{
getGameRefrence()->unregisterGameObject(this);
}
void GameObject::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived te message in GameObject");
cout << "Recived te message in GameObject";
}
CLASSTYPE GameObject::getMyClassType()
{
return _MYCURRENTCLASSTYPE;
}
void GameObject::setMyClassType(CLASSTYPE newrecip)
{
}
void GameObject::setId(int val)
{
myid = val;
}
int GameObject::getId()
{
return myid;
}
Player.h:
pragma once
//=================================
// include guard
#ifndef _PLAYER_H_
#define _PLAYER_H_
//=================================
// forward declared dependencies
//=================================
// included dependencies
#include "SystemVars.h"
#include "GameObject.h"
//=================================
// the actual class
class Player : public GameObject
{
public:
Player();
void sendMessage(string data) override;
void test();
}
Player.cpp:
Player::Player() : GameObject(CLASSTYPE_PLAYER)
{
}
void Player::sendMessage(string data)
{
//logHelperMessage(INFO,1,"Recived the message in Player");
cout << "Recived the message in Player";
//logHelperMessage(INFO,1,data.c_str());
cout << data;
}
void Player::test()
{
cout << "Sent message";
getGameRefrence()->sendMessageToAllObjects("Test",CLASSTYPE_PLAYER);
}
main.cpp
#pragma once
#include "SystemVars.cpp"
#include "Player.h"
#include "Game.h"
Game mygame;
int main(int argc, char **argv)
{
setGameRefrence(&mygame);
Player newplayer = Player();
newplayer.test();
}
Now that that is all out of the way.
The expected output is:
Sent message
Sent message from game
Recived message in Player
but insted i get:
Sent message
Sent message from game
Recived message in Gameobject
Im pretty sure i have a sliceing problem, but im not sure what to do about it, or where it is.
So, any ideas gents?
ALso, i tried to cut back the classes a bit so im not posting 2000~ lines of code. So if anyhting missing let me know.
About a dozen classes inherit from gameobject. and i need them all to talk to eachother in one way or another.
Just by having CLASSTYPE_PLAYER, etc. makes me concerned that you're trying to subvert/reinvent C++ OOP. You already have the concept of 'class' in the language, why re-invent it? Forgive me if I'm wrong. Aside from this, I suggest you look up publisher-subscriber frameworks to do the kind of thing you're after, or implement your own. I'm sure somebody will answer your C++ issue more directly.
Edit: tone of first comment.
I have a C++ class and I keep getting this error although I have another class written with similar syntax that compiles without a fuss.
Here is my .h:
#ifndef FISHPLAYER_H
#define FISHPLAYER_H
#include "Player.h"
class FishPlayer : public Player
{
public:
float xThrust;
float yThrust;
static FishPlayer* getInstance();
protected:
private:
FishPlayer();
~FishPlayer();
static FishPlayer* instance;
};
#endif
And Here is my .cpp :
#include "..\include\FishPlayer.h"
FishPlayer* FishPlayer::instance=0; // <== I Get The Error Here
FishPlayer::FishPlayer()
{
//ctor
xThrust = 15.0f;
yThrust = 6.0f;
}
FishPlayer::~FishPlayer()
{
//dtor
}
FishPlayer* FishPlayer::getInstance() { // <== I Get The Error Here
if(!instance) {
instance = new FishPlayer();
}
return instance;
}
I've been searching for a while now and it must be something so big I don't see it.
Here is the inheritance:
#ifndef PLAYER_H
#define PLAYER_H
#include "Ennemy.h"
class Player : public Ennemy
{
public:
protected:
Player();
~Player();
private:
};
#endif // PLAYER_H
And the higher one:
#ifndef ENNEMY_H
#define ENNEMY_H
#include "Doodad.h"
class Ennemy : public Doodad
{
public:
float speedX;
float maxSpeedX;
float speedY;
float maxSpeedY;
float accelerationX;
float accelerationY;
Ennemy();
~Ennemy();
protected:
private:
};
And the superclass
#include <vector>
#include <string>
enum DoodadType{FishPlayer,Player,AstroPlayer,Ennemy,DoodadT = 999};
enum DoodadRange{Close, Medium , Far};
enum EvolutionStage{Tiny, Small, Average, Large};
class Doodad
{
public:
float score;
void die();
EvolutionStage evolutionStage;
DoodadRange range;
Doodad();
virtual ~Doodad();
Doodad(Doodad const& source);
std::vector<Animation> animations;
double posX;
double posY;
std::string name;
std::string currentAnimation;
int currentFrame;
DoodadType type();
SDL_Surface getSpriteSheet();
bool moving;
void update();
protected:
private:
SDL_Surface spriteSheet;
};
Looks like you use FishPlayer as a enum value in Doodad.h
It is the same name as the class you're trying to declare. That might be the source of the problem.
Generally it's a good idea to use FISH_PLAYER, or Type_FishPlayer sort of naming scheme for enum values to avoid clashes like this.