two class different header, gcc, visual studio 2012 - c++

Why does the compiler give me an error of Card deck[5] when the two classes are in different files, but if I put them in the same file the compiler approves?
I did include ".h" to both files.
Header 1
class Card
{
private:
char* card_name;
int card_value;
Color card_color;
public:
Card();
void set_card(char * buffer , Card& c);
Card get_card(int index,Card deck[]);
int get_card_value();
void print_card();
~Card();
};
Header 2
class Player
{
private:
friend class Card;
char* name;
char* id;
int wins;
int losses;
int remains_move;
struct Coordinate cord;
Card deck[MAX_CARDS];
public:
Player();
int Player_Array_Size(char* Creation,const int MAX_PLAYERS);
void Player_Creation_File(char* Creation);
void Player_Simulation_File(char* Simulation,int Array_Size);
void set_player();
Player* get_player();
void print_player();
void move_to_point(int x ,int y);
void attack_using_card(int Attacker ,int Defender,int Attack_card);
void win_match();
void lose_match();
~Player();

You probably have 4 files, card.h, card.cpp, player.h and player.cpp.
In player.h:
#include "card.h"
class Player { ... };
In player.cpp:
#include "player.h"
...
In card.h:
class Player; // Forward declaration
class Card { ... };
In card.cpp:
#include "card.h"
#include "player.h"
...
That's it.

Related

Cocos2d-x V3 How to override ::create() properly?

I have Player class defined like this:
"Player.h"
#ifndef PLAYER_h
#define PLAYER_h
#include "cocos2d.h"
USING_NS_CC;
class Player: public Sprite {
public:
Player();
~Player();
private:
Sprite *playerSprite;
__String name;
int maxHP;
int currHP;
int maxMP;
int currMP;
int maxEXP;
int currEXP;
};
#endif
and "Player.cpp":
#include "Player.h"
I want to initialize int maxHP, int maxMP .. etc upon
Player* myPlayer = Player::create();
What would be the proper way to override ::create() function with parameters such that
// create(int maxHP, int currHP, int maxMP, int currMP, int maxEXP, int currEXP)
Player* myPlayer = Player::create(100, 100, 100, 100, 100, 100);
can be used?
You can overload create (have multiple functions with the same name but different signature).
class Player : public Sprite {
public:
static Player* create(int maxHP, int currHP, int maxMP, int currMP, int maxEXP, int currEXP) {
Player* p = Player::create();
p->maxHP = maxHP;
// etc ...
return p;
}
}

Error C2079 and Error C2440 when trying to incorporate a new type into an existing type

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.

Get attribute of an object from another class C++

I am trying to make a little poker game. In the code below I have a Game class and a Player class. The game class contains a std::vector which contains all the players. The Player class has a name attribute. Now my question is the following: how can I access the Player's attribute name through the vector that contains the Player objects? My problem appears in the last method of the code below, called show().
Thanks for helping!
//Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include "Card.h"
class Player
{
public:
Player();
Player(std::string n, double chipsQty);
private:
const std::string name;
double chipsAmount;
Card cardOne;
Card cardTwo;
};
#endif PLAYER_H
//Player.cpp
#include "Player.h"
Player::Player(){}
Player::Player(std::string n, double chipsQty) : name(n), chipsAmount(chipsQty)
{}
//Game.h
#ifndef GAME_H
#define GAME_H
#include "Player.h"
#include <vector>
class Game
{
public:
Game();
Game(int nbr, double chipsQty, std::vector<std::string> vectorNames);
void start();
void show();
private:
std::vector<Player> playersVector;
int nbrPlayers;
};
#endif GAME_H
//Game.cpp
#include "Game.h"
#include "Player.h"
Game::Game(){}
Game::Game(int nbr, double chipsQty, std::vector<std::string> vectorNames) :nbrPlayers(nbr)
{
for (int i = 0; i < vectorNames.size(); i++)
{
Player player(vectorNames[i], chipsQty);
playersVector[i] = player;
}
}
void Game::start(){};
void Game::show()
{
for (int i = 0; i < playersVector.size(); i++)
{
std::cout << playersVector[i] //Why can't I do something like playersVector[i].name here?
}
}
Because name attrbute of a Player class is private, so you cannot access it directly from another class. You should add a method to Player class that will return the name of the player, eg:
class Player
{
private:
std::string name;
public:
std::string getName() const { return name; }
};
Then you can access the player name by
playersVector[i].getName()

c++ error C2065 : undeclared identifier [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
C++ Undeclared Identifier (but it is declared?)
Im getting the error sprite.h(20): error C2065: 'Component' : undeclared identifier when I try to compile (I got a couple other files as well). Below is the sprite.h file. I cant for the life of me figure out what is causing this problem.
#ifndef SPRITE_H
#define SPRITE_H
#include "Image.h"
#include "Rectangle.h"
#include <string>
#include <SDL.h>
#include <vector>
#include "Component.h"
namespace GE2D {
class Sprite {
public:
Sprite();
Sprite(Image *i);
Sprite(Image *i, int x, int y);
Sprite(char *file, bool transparentBg, int x, int y, int w, int h);
virtual ~Sprite();
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components);
virtual void handleEvent(SDL_Event eve);
virtual void draw(SDL_Surface *screen);
void setPosition(int x, int y);
const Rectangle& getRect() const;
const Image& getImage() const;
const Sprite& operator=(const Sprite& other);
Sprite(const Sprite& other);
protected:
private:
Image image;
Rectangle rect;
};
}
#endif
In the .cpp file tick() is defined like this:
void Sprite::tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) {}
tick() is supposed to take two vectors like they do now, but maybe there's a better way to do that which might solve this problem?
EDIT
As requested, here is Component.h as well:
#ifndef COMPONENT_H
#define COMPONENT_H
#include "Rectangle.h"
#include "Component.h"
#include "Sprite.h"
#include <vector>
#include <SDL.h>
namespace GE2D {
class Component {
public:
Component();
virtual ~Component();
virtual void draw(SDL_Surface *screen) = 0;
virtual void tick(SDL_Surface *screen, std::vector<Sprite*>* sprites, std::vector<Component*>* components) = 0;
virtual void handleEvent(SDL_Event eve) = 0;
const Rectangle& getRect() const;
protected:
Component(int x, int y, int w, int h);
private:
Rectangle rect;
};
}
#endif
Sprite.h includes Component.h which includes Sprite.h, giving a circular dependency which can't be resolved.
Luckily, you don't need to include the headers at all. Each class only refers to a pointer to the other class, and for that a simple declaration is enough:
class Component;

error: expected constructor, destructor, or type conversion before '*' token

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.