Classes in C++ Constructor and D'tor - 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.I'm not sure though how to make the constructor and destructor of the Class Game. This is my first try but the code isn't working any idea?
Game::Game(int maxPlayer)
{ this->playersArray = new Player*[maxPlayer];
for(int i=0;i<maxPlayer;i++)
{
playersArray[i]=NULL;
}
}
Game::~Game() {
for(int i=0;i<maxPlayer;i++)
{
delete[] *playersArray[i];
}
delete (playersArray);
}

It has been along time since I have programmed in C++. The only error I see for sure is that the constructor has a parameter maxPlayer and never touches the member variable maxPlayer. The destructor uses the member variable maxPlayer, which probably has never been initialized. The constructor ought to assign the parameter to the member variable.
When you delete an array you are supposed to use delete[] but you use delete for a single object. So, I think you have them swapped.

OK, since I did not get response to my comment, let me try this: If you modify your code as shown below, does it do what you expect it to do?
class Player
{
};
class Game
{
private:
int maxPlayer;
Player** playersArray;
public:
Game(int maxPlayer);
~Game();
};
Game::Game(int maxPlayer)
{
this->maxPlayer = maxPlayer;
this->playersArray = new Player*[maxPlayer];
for (int i = 0;i<maxPlayer;i++)
{
playersArray[i] = NULL;
}
}
Game::~Game() {
for (int i = 0;i<maxPlayer;i++)
{
delete playersArray[i];
}
delete[] playersArray;
}
int main()
{
Game g(10);
return 0;
}

Related

C++ copy object

I have a class Game which contains an other object GameGrid:
Game.h
class Game
{
public:
Game(uint32_t GridSize);
private:
GameGrid _CurrentGrid;
}
Game.cpp
Game::Game(uint32_t GridSize) : _CurrentGrid(GridSize)
{
}
GameGrid.h
class GameGrid
{
public:
GameGrid(uint32_t GridSize);
void setGrid(const Grid& Grid);
const GameGrid::Grid getGrid(void) const;
private:
Grid _Grid;
}
GameGrid.cpp
GameGrid::GameGrid(uint32_t GridSize)
{
uint32_t i = 0;
uint32_t j = 0;
_Grid.assign(GridSize, std::vector<unsigned int>(GridSize));
for (i = 0; i < GridSize; i++)
{
for (j = 0; j < GridSize; j++)
{
_Grid.at(i).at(j) = 0;
}
}
}
void GameGrid::setGrid(const GameGrid::Grid& Grid)
{
_Grid = Grid;
}
const GameGrid::Grid GameGrid::getGrid(void) const
{
return _Grid;
}
Now I have my application, which uses the game class
Game* MyGame = new Game(4);
How can I create a copy function for this pointer to the Game-Object, so that I can clone the object.
I´ve tried it with the assignment operator
Game& operator=(Game const& Ref);
Game& Game::operator=(Game const& Ref)
{
if (&Ref != this)
{
this->~Game2048();
new (this) Game2048(Ref);
}
return *this;
}
But this solution doesn´t work and my original object got changed too, when I change the clone.
Does someone has a hint for me?
Thank you!
Don't create the copy constructor yourself and let the compiler do it for you, then just use objects:
Game MyGame{4};
Game gameClone = MyGame;
There's no reason to use a raw pointer here.
I agree to what #SombreroChicken hast said.
I just want to point out the difference between a deep copy and shallow copy.
If your class Contains some pointer as private member data, you have to be aware, that only the pointer is copied and not the data to which is being pointed. Here is a nice in depth explanation of what I said.

Call member function on object pointer

I am trying to write a simple game in C++ and currently have my Game_Window class holding an array of pointers to game objects as follows:
class Game_Window {
private:
int width;
int height;
int num_objects;
public:
char** objects;
/* The rest of the class goes here */
}
Inside my Game_Window class I want to define a function that calls the "print()" function on all objects held in the game window "objects" array as follows.
void Game_Window::print_objects() {
for (int i = 0; i < num_objects; i++) {
(objects[i])->print(); /* THE PROBLEM IS HERE */
}
}
When I compile I get the following error:
game_window.cpp:29:15: error: member reference base type 'char' is not a structure or union
(objects[i])->print();
~~~~~~~~~~~~^ ~~~~~
1 error generated.
All objects in my game have a "print()" function so I know that's not the issue. Any help would be much appreciated.
I think I figured it out. I created a class called Game_Object that all my game objects will inherit, and gave it a print() method.
class Game_Object {
private:
Location location;
public:
Game_Object();
Location *get_location() { return &location; }
void print();
};
class Diver : public Game_Object {
public:
explicit Diver(int x, int y);
};
class Game_Window {
private:
int width;
int height;
int num_objects;
public:
Game_Object** objects;
explicit Game_Window(int width, int height);
~Game_Window();
int get_width() { return width; }
int get_height() { return height; }
int get_object_count() { return num_objects; }
bool add_object(Game_Object object);
void print_objects();
};
The invocation of print() is now:
void Game_Window::print_objects() {
for (int i = 0; i < num_objects; i++) {
objects[i]->print();
}
}
I ran it and it game me no errors.
The type of Game_Window::objects is char** (pointer to pointer to char). Hence objects[i] is the ith pointer, and pointers don't have print() methods, which is why (objects[i])->print(); fails with the described error.
Perhaps you meant to use print(objects[i]); instead?

Classes accessing each other with pointers

I'm still confused with pointers, maybe you could enlighten me a bit.
I'm having some classes pointing to each other and don't get the way to access them in the right way.
#include <vector>
#include <iostream>
class Player;
class Trainer;
class Team {
Trainer* trainer_;
std::vector<Player*> player_;
public:
std::vector<Player*> player() { return player_; }
Trainer* trainer() { return trainer_; }
std::vector<Player*> get_playerlist(){
return player_;
};
};
class Player {
public:
void setteam_(Team* x){
team_ = x;
}
private:
Team* team_;
};
class Trainer {
Team* Team_;
};
int main()
{
Player player1;
Team team1;
std::vector<Player*> playerlist;
player1.setteam_(&team1);
playerlist = team1.get_playerlist();
std::cout << playerlist.size();
std::cin.get();
return 0;
}
In the main there is player1 created and assigned to the playerlist of team1, now he should somehow appear there when i get the playerlist. But the output of this code is that the size of the playerlist of team 1 is still 0. What did I do wrong ?
Your Team class has no method that actually adds players to the player_ vector. Setting the team_ pointer to the team1 instance is not sufficient.
player1.setteam_(&team1);
What happens next is:
void setteam_(Team* x){
team_ = x;
}
Here you are stating that this Player has pointer to Team set to the same address as x. But this doesn't mean that this Team object has pointer to player in std::vector. This doesn't happen automaticly, you need something like:
void setteam_(Team* x){
team_ = x;
x->addPlayer( this); // add to vector
}

Virtual function issue in C++ [duplicate]

This question already has answers here:
Why is virtual function not being called?
(6 answers)
Closed 9 years ago.
AoA,
I am making a console game of chess, But I am stuck at polymorphism, below is the classes and functions definitions
/* old Part
//Base Class
class Piece /*Parent class */
{
protected:
Position* pCoord;
std::string color;
char symbol;
public:
Piece(Position* Coord,std::string Color,char symbol);
Position GetCurrentPos();
std::string GetColor();
void SetColor(std::string color);
void Draw();
virtual bool SetPos(Position* newPos){MessageBox(NULL,L"Virtual Running",L"Error",MB_OK); return true;};
virtual ~Piece();
};
/* Inherited classes */
//Child classes
class Pawn: public Piece
{
private:
std::vector<Position>* allowPos;
public:
Pawn(Position* Coord,std::string Color,char symbol);
~Pawn();
std::vector<Position>* GetThreatendFields();
bool isValidMove(Position* newPos);
bool SetPos(Position* newPos);
};
//Child classes
class Bishops: public Piece
{
private:
std::vector<Position>* allowPos;
public:
Bishops(Position* Coord,std::string Color,char symbol);
~Bishops();
std::vector<Position>* GetThreatendFields();
bool isValidMove(Position* newPos);
bool SetPos(Position* newPos);
};
//Here is the implementation of child class function SetPos
bool Pawn::SetPos(Position* newPos)
{
bool isSet = false;
this->pCoord = new Position();
this->pCoord = newPos;
isSet = true;
MessageBox(NULL,L"Child function running",L"Yuhuu!",MB_OK);
return isSet;
}
class ChessBoard
{
private:
Position ptr; //dummy
int SelectedPiece;
vector<Piece> pPieceSet;
bool isSelected;
public:
ChessBoard();
~ChessBoard();
void ShowPieces(Player *p1,Player *p2);
void Draw();
void MouseActivity();
void Place(Piece& p);
};
//it just shows the peices acquired from player objects..dummy vector pointer
void ChessBoard::ShowPieces(Player* p1,Player* p2)
{
std::vector<Piece>* vPiece = p1->GetPieces();
for( int i=0;i<vPiece->size();i++ )
{
Piece& piece = vPiece->at(i);
Place(piece);
piece.Draw();
}
vPiece = p2->GetPieces();
for( int i=0;i<vPiece->size();i++ )
{
Piece& piece = vPiece->at(i);
Place(piece);
piece.Draw();
}
}
*/
/*new part
I did what you say
Player::std::vector<Piece*> *vPieceSet;
Player::Player(int turn)
{
this->turn = turn%2;
this->vPieceSet = new std::vector<Piece*>;
}
void Player::Initialize() //Initial and final ranges for position
{
//Initialization of pieces to their respective position
Position pos;
Piece *pPiece;
if( this->turn == 0 )
{
this->SetName("Player 1");
for( int i=8;i<16;i++ )
{
pos.SetPosition(i);
Pawn pPawn(&pos,"blue",'P');
pPiece = &pPawn;
this->vPieceSet->push_back(pPiece);
}
//other classes same as above
}
It runs fine at initialzation function(stores all classes fine) but when use function to get the vector object
std::vector<Piece*>* Player::GetPieces()
{
std::vector<Piece*>* tPieces = this->vPieceSet;
return tPieces;
}
//In main.cpp
it doesnot return the vector object
Player p1(0),p2(1);
p1.Initialize();
p2.Initialize(); //initialization done perfectly while debugging
vector<Piece*> *obj = p1.GetPieces(); //returns garbage
Piece* pObj = obj->at(0); //garbage
cout<<pObj->GetColor(); // garbage
*/new part
Sounds like I have another problem!
When you use polymorphism, what you are really trying to do is instantiate an object of derived type and call the methods on that object through a pointer or reference to the base object.
class Foo
{
public:
virtual void DoIt () { cout << "Foo"; }
};
class Bar
:
public Foo
{
public:
void DoIt () { cout << "Bar"; }
};
int main()
{
Foo* foo = new Bar;
foo->DoIt(); // OUTPUT = "Bar"
Foo& fooRef = *foo;
fooRef.DoIt(); // OUTPUT = "Bar"
}
In order for this to work, you need to use either a pointer or a reference to the object. You can't make a copy of the object using a the base class. If you make a copy, you will slice the object.
int main()
{
Foo* foo = new Bar;
foo->DoIt(); // OK, output = "Bar"
Foo fooCopy = *foo; // OOPS! sliced Bar
fooCopy.DoIt(); // WRONG -- output = "Foo"
}
In your code, the Piece class is intended to be polymorphic, and in your ChessBoard class you have a vector of this class:
class ChessBoard
{
private:
vector<Piece> pPieceSet;
};
Since this is a vector of the Piece object itself, and not a pointer-to-Piece, anything you put in here will be sliced. You need to change pPieceSet to be a vector of pointers-to-Piece:
vector <Piece*> pPieceSet;
You have further problems in Initialize, which need to be refactored anyway. For one thing, you have another vector of Piece objects, and there are two problems here. First, it needs to be a vector of pointers, and second, why do you need another vector at all when there is already one associated with the ChessBoard? I didn't thouroughly examine your code so maybe you do need it, but this seems like an error. There should probably just be one collection of pieces, in the ChessBoard.
In your Initialize method:
Piece *pPiece;
// ...
Pawn pPawn(&pos,"blue",'P');
pPiece = &pPawn;
vPieceSet.push_back(*pPiece);
There are a couple of problems. One, you are pushing back a sliced copy of the Piece, which will be fixed when you change your vector to store pointers. Second, if you just change this like so:
Piece *pPiece;
// ...
Pawn pPawn(&pos,"blue",'P');
pPiece = &pPawn;
vPieceSet.push_back(pPiece); // <-- not dereferencing
You will have a new problem because you'll be storing the pointer to a local (automatic) variable. Best is to do this:
Piece* pPiece = new Pawn (...);
// ...
vPieceSet.push_back (pPiece);
Please don't forget to delete everything you new. This is best handled by using smart pointers rather than raw pointers. In C++03 we have auto_ptr, but those can't go in a vector. Instead you'll need to use Boost or something else, or just store raw pointers. In C++11, we now have unique_ptr (preferred) and shared_ptr, which can go in to a vector.
In C++11, the best solution here is to have a vector declared as:
vector <unique_ptr <Piece> > pPieceSet;
...unless you have some compelling need to use shared_ptr instead.
As others have mentioned, it is a slicing issue, and the issue is created here:
class Player
{
private:
std::string pName;
std::vector<Piece> vPieceSet; // <-- This is your problem
int turn;
public:
Player(int turn);
~Player();
void Initialize();
std::string GetName();
void SetName(std::string Name);
int GetTurn();
std::vector<Piece>* GetPieces();
};
You are storing them in the vector as instances of Piece, which is slicing off the details of the piece (e.g. the Bishop implementation). You should modify it to something like:
class Player
{
private:
std::string pName;
std::vector<Piece*> vPieceSet; // or better, use a smart pointer wrapper
int turn;
public:
Player(int turn);
~Player();
void Initialize();
std::string GetName();
void SetName(std::string Name);
int GetTurn();
std::vector<Piece*> GetPieces(); // note this change as well
};
With your additional question/edit, you are getting another unrelated problem:
void Player::Initialize() //Initial and final ranges for position
{
Position pos; // position is declared inside the scope of Initialize
Piece *pPiece;
if( this->turn == 0 )
{
this->SetName("Player 1");
for( int i=8;i<16;i++ )
{
pos.SetPosition(i);
Pawn pPawn(&pos,"blue",'P'); // you are passing the address of position to the Pawn, and Pawn is within the scope of this loop
pPiece = &pPawn; // you are storing the address of the Pawn
this->vPieceSet->push_back(pPiece);
}
// Pawn is now out of scope and pPiece points to the memory location Pawn *used* to be at (but will likely be overwritten soon).
// As soon as this function returns, you have the same problem with pos
}
You need to allocate those variables on the heap (hence the reason we suggested smart pointer wrappers).

why is deleting this object causing problems?

Instantiation:
weapons.push_back(new Pistol());
weapons.push_back(new Rifle());
weapons.push_back(new Shotgun());
destructor, when the first delete happens, the code breaks. This happens when I close the program.
Brain::~Brain()
{
for (unsigned int i = 0; i < weapons.size(); i++)
{
delete weapons[i]; // this is where the code breaks
}
}
I get a warning:
Unhandled exception at 0x0096371f in D3D10DEMO.exe: 0xC0000005: Access violation reading location 0x000002ce.
weapons is this:
weapons(vector<Gun*>())
Edit - I have deleted much of the code from this question but I have also cut down my program so as to reproduce the problem in a much smaller solution here:
http://dl.dropbox.com/u/13519335/D3D10DEMO_0.25.MinRep.zip
You haven't defined virtual destructors for your weapon classes.
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7
You problem is the definition of
class Brain : public Entity
{
private:
std::vector<Gun*> weapons;
and the ownership of Gun* by Brain object.
Brain::~Brain()
{
for (unsigned int i = 0; i < weapons.size(); i++)
{
delete weapons[i];
}
}
If a Brain is copy constructed the delete will be called multiple times deleting the same Gun from different weapons vector. And a Brain temporary is created when you add your Agents (Agent being a derived class of Brain) like so in main function.
int main()
{
Level* level;
std::vector<Agent> agents;
level = new Level(agents);
for (int i = 0; i < 1; i++)
{
//TODO - health is a pointless parameter here
agents.push_back(Agent(100, *level, agents, level->Pickups(), D3DXCOLOR(1.0F, 0.4f, 0.4f, 1.0f)));
}
delete level;
}
 If you implement a copy constructor for Brain that clones the Gun* vector you should be ok. Alternative you should use shared_ptr<Gun> in your vector so that you don't have to delete them at all.
To summarize your problem boils down to
class Foo{};
class Bar
{
public:
Bar()
{
mFooVec.push_back( new Foo() );
mFooVec.push_back( new Foo() );
}
~Bar()
{
for( unsigned int i = 0;i < mFooVec.size(); ++i )
{
delete mFooVec[i];
}
}
std::vector<Foo*> mFooVec;
};
int main()
{
Bar x;
Bar y = x;
return 0;
}
Here both Bar x and y have the same two Foo* in their mFooVec