C ++ OOP - Data Loss

I have some simple classes and I can't seem to get them to work.

TL; DR I have an instance of "Player", after I have set some data to the instance, I can get it back. If I push the instance to std :: vector Players; if i have Players.at (0) .getName () it returns ". No data! Gone. (Debug application I see" _name "set to" vPlayer "and in" Players "I see an item with" _name "=" ")

Here is the code:

//Player.h
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>

class Player
{
public:
    Player();
    Player(const Player &Player);
    Player& operator=(const Player &Player);
    std::string getName();
    bool        setName(const std::string &name);
    bool        nameValid(const std::string &name);

private:
    std::string _name;
};



#endif



//Player.cpp

#include "Player.h"
#include <iostream>
#include <string>
using namespace std;

Player::Player()
{

}
Player::Player(const Player &Player)
{

}
Player& Player::operator=(const Player &Player) {
    return *this;
}

std::string Player::getName()
{
    return this->_name;
}

bool Player::setName(const std::string &name)
{
    if ( ! this->nameValid(name) )
    {
        return false;
    }

    this->_name = name;
    return true;
}

bool Player::nameValid(const std::string &name)
{
    return name.empty() == false;
}




//Map.h
#ifndef MAP_H
#define MAP_H

#define MAP_X 40
#define MAP_Y 40

#include "Player.h"
#include "Point.h"
#include <vector>

class Map
{
public:
    Map();
    bool movePlayer(Player &Player, Point &Point);
    std::vector<Player> getPlayers();
private:

};

#endif //MAP_H



//Map.cpp

#include "Map.h"
#include "Player.h"
#include "Point.h"
#include <iostream>
#include <string>

using namespace std;

Map::Map()
{

}

bool Map::movePlayer(Player &Player, Point &Point)
{
    return true;
}
std::vector<Player> Map::getPlayers()
{
    Player vPlayer;
    vPlayer.setName(std::string("test"));
    std::vector<Player> Players;

    Players.push_back(vPlayer);

    return Players;
}

      

basically:

  std::vector<Player> Players = vMap.getPlayers();
  cout<<"Test:"<<Players.at(0).getName()<<endl;

      

+3


source to share


2 answers


Your vector will contain copies of the elements that you add to it. These copies will be added using the constructor Player :: Player (const Player &).

This constructor (in your implementation) does not set a value for the name.

Solutions:



  • set a name in the copied object:

    Player::Player(const Player &Player) : _name(Player._name) { }

(The same is true for your assignment operator)

  • Remove copy and assign functionality and rely on the default. Since the name is std :: string, it will by default get a copy of the original player name.
+4


source


You define an instance copy constructor and a copy assignment operator to do nothing. How do you expect the copy in the vector to have the same data as the instance you injected into the vector?



Your class can handle the compiler's default copy constructor and copy assignment operator just fine, so just remove your declarations and definitions for them and everything will work.

+10


source







All Articles