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;
}
}
Related
I'm quite new to c++ and I started looking at the SDL2 library to make a simple platformer game. I'm running into a problem with class inheritance. I have a parent class (GameObj) and a child class (Player). When I try to inherit the the GameObj from Player the compiler tells me the base class is undefined. I am trying to override the update function in the Player class, however since I get a "Base class undefined" error it also tells me that the overrode function did not override anything.
GameObj.h
#pragma once
#include "Game.h"
class GameObj
{
public:
GameObj(const char* path);
~GameObj();
virtual void update() = 0;
void render();
protected:
SDL_Texture* tex{ nullptr };
SDL_Rect srcR;
SDL_Rect dstR;
};
GameObj.cpp
#include "GameObj.h"
GameObj::GameObj(const char* path)
{
tex = TextureManager::LoadImage(path);
srcR.x = srcR.y = 0;
srcR.w = srcR.h = 32 * 2;
dstR.x = dstR.y = 0;
dstR.w = dstR.h = srcR.w;
}
GameObj::~GameObj() {}
void GameObj::render() {
TextureManager::DrawTexture(tex, srcR, dstR);
}
Player.h
#pragma once
#include "Game.h"
#include "GameObj.h"
class Player : public GameObj
{
public:
Player(const char* path, int x, int y);
~Player();
void update() override;
private:
int x{ 0 };
int y{ 0 };
int speed{ 0 };
int velocity{ 0 };
};
Player.cpp
#include "Player.h"
Player::Player(const char* path, int x, int y) : GameObj(path){};
Player::~Player() {};
void Player::update ()
{
x++;
dstR.x = x;
dstR.y = y;
}
As Retired Ninja commented there is a circular include issue in the code. GameObj.h included Game.h and Game.h included Player.h. To fix this issue I removed the Game.h include in GameObj.h.
GameObj.h
#pragma once
#include <SDL.h>
class GameObj
{
public:
GameObj(const char* path);
virtual ~GameObj() = default;
virtual void update() = 0;
void render();
protected:
SDL_Texture* tex{ nullptr };
SDL_Rect srcR;
SDL_Rect dstR;
};
GameObj.cpp
#include "GameObj.h"
#include "TextureManager.h"
GameObj::GameObj(const char* path)
{
tex = TextureManager::LoadImage(path);
srcR.x = srcR.y = 0;
srcR.w = srcR.h = 32 * 2;
dstR.x = dstR.y = 0;
dstR.w = dstR.h = srcR.w;
}
GameObj::~GameObj() {}
void GameObj::render() {
TextureManager::DrawTexture(tex, srcR, dstR);
}
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.
I'm receiving error "Allocating an object of abstract class type 'MainGame" even though all virtual functions have been implemented. Below are the relevant code snippets:
main.cpp
#include "Gamestate_MainGame.h"
int main() {
Game game;
if (game.init(new MainGame))
game.loop();
return 0;
}
Gamestate.h
#ifndef Gamestate_h
#define Gamestate_h
#include <SDL2/SDL.h>
#include "Game.h"
class GameState {
public:
virtual bool init(Graphics* graphics, Game* game) = 0;
virtual void quit() = 0;
virtual void handleEvents(SDL_Event* e) = 0;
virtual void logic() = 0;
virtual void render() = 0;
protected:
Game* game = NULL;
Graphics* graphics = NULL;
};
#endif
Gamestate_MainGame.h
#ifndef Gamestate_MainGame_h
#define Gamestate_MainGame_h
#include <vector>
#include <SDL2_mixer/SDL_mixer.h>
#include "Gamestate.h"
#include "Graphics.h"
#include "Tile.h"
class MainGame : public GameState {
// Gamestate Functions
bool init(Graphics* graphics, Game* game);
void quit();
void handleEvents(SDL_Event& e);
void logic();
void render();
// MainGame functions - make private?
void makeTiles();
void loadPositions(const int & n);
void scrambleTiles(std::vector<Tile> t);
void restart();
bool isSolved();
bool isNeighbor(const Tile& a, const Tile& b);
int getClickedTile(const int& x, const int& y);
private:
Game* game = NULL;
Graphics* graphics = NULL;
int clickedTile { -1 };
int clicks { 0 };
bool gameExit { false };
bool gameWin { true };
bool catMode { true };
std::vector<Tile> tiles;
std::vector<SDL_Rect> positions;
Mix_Chunk* click = NULL;
};
#endif
Game.h
#ifndef Game_h
#define Game_h
#include <vector>
#include <SDL2/SDL.h>
#include "Graphics.h"
class GameState;
class Game {
public:
Game();
bool init(GameState* state);
void loop();
void pushState(GameState* state);
void popState();
void setQuit();
private:
bool quit { false };
Graphics graphics;
SDL_Event event;
std::vector<GameState*> states;
Uint32 new_time;
Uint32 old_time;
//internal loop functions
void update();
void render();
void quitGame(); //will free SDL resources and perform cleanup of states
};
#endif
All MainGame functions are defined in Gamestate_MainGame.cpp. Thank you for your help!
virtual void handleEvents(SDL_Event* e) = 0;
^
is a different prototype than
void handleEvents(SDL_Event& e);
^
You should mark all your overridden functions with override then the compiler will do the heavy lifting for you and tell you whether you messed up or not.
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()
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.