我有一些简单的课程,但我无法让它们工作。
TL;DR 我有一个“播放器”实例,在我为实例设置了一些数据后,我可以取回它。如果我将实例推送到std::vector Players; 如果我有 Players.at(0).getName() 它返回“”。数据不存在!离开了。(调试应用程序时,我看到在“vPlayer”中设置了“_name”,在“Players”中我看到了一个元素,其中“_name”=“”)
这是代码:
//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;
}
主要:
std::vector<Player> Players = vMap.getPlayers();
cout<<"Test:"<<Players.at(0).getName()<<endl;