Constructor in C++ Class - c++

I have two Classes, Player and Game.
class Game
{
private:
int maxPlayer;
Player** playersArray;
public:
Game(int maxPlayer);
~Game();
}
Each index in playersArray consists of pointers to class Player.The following constructor doesn't work, since this message keeps appearing:
error: invalid use of 'Player::Player' playersArray[i]->Player();
Game::Game(int maxPlayer)
{ this->maxPlayer=maxPlayer;
this->playersArray = new Player*[maxPlayer];
for(int i=0;i<maxPlayer;i++)
{
playersArray[i]->Player();
}
}
This is the class Player:
class Player {
private:
char* player_name;
int level;
int life;
int strength;
Weapon player_weapon;
int place;
}
My aim is to set the player_name to NULL and NOT to a random place in the memory. This is what Player() is supposed to do.

You are not populating your Game array correctly. You are trying to call the Player() constructor as if it were a regular class method (which it is not), and worse you are calling it via an uninitialized Player* pointer.
You need to use the new operator instead, eg:
class Game
{
private:
int maxPlayers;
Player** playersArray;
public:
Game(int aMaxPlayers);
~Game();
}
Game::Game(int aMaxPlayers)
{
maxPlayers = aMaxPlayers;
playersArray = new Player*[maxPlayers];
for(int i = 0; i < maxPlayers; ++i)
playersArray[i] = new Player; // <-- here
}
Game::~Game()
{
for(int i = 0; i < maxPlayers; ++i)
delete playersArray[i];
delete[] playersArray;
}
A safer option is to use std::unique_ptr instead of raw pointers:
#include <memory>
class Game
{
private:
int maxPlayers;
std::unique_ptr<std::unique_ptr<Player>[]> playersArray;
public:
Game(int aMaxPlayers);
}
Game::Game(int aMaxPlayers)
{
maxPlayers = aMaxPlayers;
playersArray = std::make_unique<std::unique_ptr<Player>[]>(maxPlayers);
for(int i = 0; i < maxPlayers; ++i)
playersArray[i] = std::make_unique<Player>();
}
That being said, there is no need to use an array of Player* pointers when an array of Player objects will suffice instead:
class Game
{
private:
int maxPlayers;
Player* playersArray;
public:
Game(int aMaxPlayers);
~Game();
}
Game::Game(int aMaxPlayers)
{
maxPlayers = aMaxPlayers;
playersArray = new Player[maxPlayers];
}
Game::~Game()
{
delete[] playersArray;
}
Or:
#include <memory>
class Game
{
private:
int maxPlayers;
std::unique_ptr<Player[]> playersArray;
public:
Game(int aMaxPlayers);
}
Game::Game(int aMaxPlayers)
{
maxPlayers = aMaxPlayers;
playersArray = std::make_unique<Player[]>(maxPlayers);
}
Which you can then simplify further by using std::vector instead:
#include <vector>
class Game
{
private:
std::vector<Player> playersArray;
public:
Game(int maxPlayers);
}
Game::Game(int maxPlayers)
: playersArray(maxPlayers)
{
}

Related

Not able to modify class composite data members

I have 3 classes in my program that interact with each other and contain each other's instances:
class Inventory
{
public:
// Increment Data Members
void incrementHerbs() { herbs++; }
void incrementHealth() { health++; }
void incrementGold() { gold++; }
// Getters
int getHerbs() { return herbs; }
int getHealth() { return health; }
int getGold() { return gold; }
private:
int herbs = 0;
int health = 3;
int gold = 0;
};
class Player
{
public:
void setRow(int row) { this->rowCoordinate = row; }
void setCol(int col) { this->colCoordinate = col; }
int getRow() { return rowCoordinate; }
int getCol() { return colCoordinate; }
Inventory getBag() { return Bag; }
private:
int rowCoordinate;
int colCoordinate;
Inventory Bag;
};
class Board
{
public:
int getNumRows() { return numRows; }
int getNumCols() { return numCols; }
Player getPlayer() { return User; }
private:
int numRows;
int numCols;
char** maze;
Player User;
};
I am only instantiating a Board object in the main function. At a point in my program, I want to be able to increment the herb count in the inventory class through that object.
I have tried doing:
Board board;
board.getPlayer().getBag().incrementHerbs();
This call compiles without any errors but when I print out the herb count afterwards, the herb count is still the same.
It did not increment. What can be going wrong and what can I do?
What can be going wrong and what can I do?
In your Player class, your getBag() function returns a copy of Inventory (i.e. member Bag).
Inventory getBag() { return Bag; }
//^^^^^^----> is copy!
You need to return the reference in order to modify it
Inventory& getBag() { return Bag; }
//^^^^^^^^
The same issue with the Board's function getPlayer()
Player getPlayer() { return User; }
//^^^^----> is copy!
you need
Player& getPlayer() { return User; }
//^^^^^^
And here's a demo.

C++ class inherit (game structure)

I'm trying to make some game engine (I know, should do an actual game instead).
Sadly have defeat at project structure. it looks like this (footage from my fifth attempt):
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
class Player;
class Engine {
private:
CHAR_INFO *chiBuffer;
int width = 0;
int height = 0;
vector <Player> players;
public:
Engine(int x = 120, int y = 30) {
chiBuffer = new CHAR_INFO[x*y];
}
void addPlayer(Player player) {
players.push_back(player);
}
void render() {
for (int i = 0; i < players.size(); i++) {
Player *player = &players[i];
player->draw();
}
}
protected:
inline CHAR_INFO& getPixel(int x, int y) { return chiBuffer[x + width * y]; }
};
class Drawable : Engine {
protected:
inline CHAR_INFO& getPixel(int x, int y) { return Engine::getPixel(x, y); }
virtual void draw() = 0;
};
class Player : Drawable {
protected:
int x = 0;
int y = 0;
public:
void draw() {
getPixel(x, y).Char.UnicodeChar = L'*';
}
};
int main() {
Engine *eng = new Engine(120, 30);
Player p;
eng->addPlayer(p);
return 0;
}
I would like to make it easily extensible, so in my mind had been born an idea of creating master class (Engine) sub class Drawable which will have draw() method and later Tickable which would have onTick() etc... But I'm pretty sure I'm doing it so wrong. Could you tell me better idea of doing this? Or make this just working because this gives me too many errors to write it right here (VS 2017)
First, why use this ?
for (int i = 0; i < players.size(); i++) {
Player *player = &players[i];
player->draw();
}
It's the same as: for(...) { players[i].draw() ;}.
Don't use variables and pointers where you don't need to (like in your main, don't use the new keyword: it's pointless. Engine e = Engine(120,30) is ok.).
The for loop on a vector should use Iterators :
for (auto it = begin (vector); it != end (vector); ++it) {
it->draw;
}
In manners of good practice, you should write your code in different files.
Engine.h for Engine declaration, Engine.cpp for implementation.
Same for Player.

Segmentation fault while trying to change game state

I'm making a state machine for switching game states (playing->menu->setup) in my game engine, but I'm getting a segmentation fault. I can change the game state fine from upper level, but how can I change the game state from within a game state?
Here's a minimal code example:
#include <iostream>
#include <vector>
class GameStateManager;
class GameState {
public:
GameState(GameStateManager* StateManager) {
StateManager = stateManager;
};
virtual ~GameState();
virtual void update() = 0;
GameStateManager* stateManager;
};
class GameStateManager {
public:
GameStateManager();
~GameStateManager();
void changeGameState(GameState* state) {
if(!running) {
running = true;
}
// Cleanup the current state
if(!gameStates.empty()) {
for(unsigned int i = 0; i < gameStates.size(); i++) {
delete gameStates[i];
}
gameStates.clear();
std::cout << "Cleaning up GameState" << std::endl;
}
// Store and initialize the new game state
gameStates.push_back(state);
};
void update() {
if(!gameStates.empty()) {
gameStates.back()->update();
}
};
std::vector<GameState*> gameStates;
bool running;
};
class PlayState : public GameState {
public:
PlayState(GameStateManager* stateManager) : GameState(stateManager) {};
~PlayState();
void update() override {
// On some flag, initiate the next level
nextLevel();
};
void nextLevel() {
stateManager->changeGameState(new PlayState(stateManager));
};
};
int main() {
GameStateManager stateManager;
stateManager.changeGameState(new PlayState(&stateManager));
while(stateManager.running) {
for(unsigned int i = 0; i < 10000; i++) {
std::cout << "Round: " << i << std::endl;
// Segmentation fault here
stateManager.update();
// This works
//stateManager.changeGameState(new PlayState(&stateManager));
}
stateManager.running = false;
}
return 0;
}
for some reason the stateManager is not being set in the GameState constructor.
so try this:
class GameState {
public:
GameState(GameStateManager* StateManager) : stateManager(StateManager)
{
};
virtual ~GameState();
virtual void update() = 0;
GameStateManager* stateManager;
};

how to create and delete a new 2d vector of object pointers

I'm not sure if I'm doing this right, I feel like there should be a simpler way to do it. I'm trying to create a new two dimensional vector of objects in a constructor. my compiler seems to be ok with my code, but I want to be sure I'm not causing any memory leaks.
here's my code:
#include <iostream>
#include <vector>
using namespace std;
//prototypes
class node;
class link;
class node
{
public:
vector<link*> links;
~node()
{
for(unsigned int i=0;i<links.size();i++)
{
delete (links[i]);
}
links.clear();
}
};
class link
{
public:
node &origin;
};
class network
{
public:
network( int dimensions[])
{
for (int i=0; i < (sizeof(dimensions)/sizeof(*dimensions)); i++)
{
vector<node*> *nvec= new vector<node*>;
for (int j=0; j <dimensions[i]; j++)
{
(*nvec).push_back(new node);
}
layers.push_back(*nvec);
}
}
//cleanup
~network()
{
for(row=layers.begin(); row!=layers.end(); row++)
{
for(col=row->begin(); col !=row->end(); col++)
{
delete(*col); //im hoping this deletes the node not the iterator
}
row->clear();//will this clear the row it points to ?
}
layers.clear();
}
private:
vector<vector <node*>> layers;
vector<node*>::iterator col;
vector<vector<node*>>::iterator row;
};
int main()
{
int arr[]={1,1,1};
network net(arr);
return 0;
}

Dynamic object array increase

I'm building a domino game and trying to make a function that adds a Stone to a pile.
This is the Stone class:
class Stone
{
public:
Stone(){ left = 0; right = 0; };
Stone(int x, int y){ left = x; right = y; };
void turnStone();
bool comStones(const Stone&);
void printOpen();
void printClosed();
friend class Pile;
friend class Game;
private:
int left, right;
};
And this is the Pile class:
class Pile
{
public:
Pile(){};
Stone indexfun(int);
void printOpen();
void printClosed();
void fillPile();
void randPile();
void addStone(Stone&);
void remStone(Stone&);
friend class Player;
friend class Game;
private:
Stone* list;
int size=0;
};
Now the function I built for adding a stone to the pile is:
void Pile::addStone(Stone& newStone) {
Stone* newList = new Stone[size + 1];
int i;
for (i = 0; i < size; i++)
{
newList[i] = list[i];
}
newList[i] = newStone;
delete[] list;
list = newList;
size++;
}
When I try and build the newlist array it throws me out of the program. can't find the reason. Help anyone?
the previous functions and more classes in the project:
class Game
{
public:
Game(char*);
void run();
private:
Player human, computer;
Pile box, table;
int start, finish;
void playerMove(int);
void checkPile(int);
bool checkMove(int,int);
bool whosFirst();
bool checkGame();
void printTable();
};
class Player
{
public:
Player(){};
void addStone(Stone&);
void remStone(Stone&);
void printPile();
Stone* searchStone();
friend class Game;
private:
char* name;
Pile pie;
};
void Game::run() {
cout << "starting the game" << endl;
box.fillPile();
box.size = 28;
box.randPile();
for (int i = 0; i < 7; i++)
{
human.addStone(box.list[i]);
box.remStone(box.list[i]);
computer.addStone(box.list[i]);
box.remStone(box.list[i]);
}
}
void Player::addStone(Stone& added) {
pie.addStone(added);
}
int main()
{
char name[80];
cout << "enter your name" << endl;
cin >> name;
Game game(name);
game.run();
}
Maybe change the "list" variable name to "list1" or something like that. "list" is a STL container, just like vector.