我创建了一个单例对象,它包含一组列表。主要是一个包含播放器对象的向量列表。
这允许我在不同的类之间发送一个玩家列表。
但是,当我试图编辑这些播放器对象中的信息,例如分数或转弯时,它不允许我这样做。
代码示例和输出:
u.CreateStaticPlayers();
cout << "turns" << d->getPlayers().at(1).getTurns() << endl;
d->getPlayers().at(1).setTurns(3);
cout << "turns" << d->getPlayers().at(1).getTurns() << endl;
cout << "score" << d->getPlayers().at(1).getScore() << endl;
d->getPlayers().at(1).setScore(11);
cout << "score" << d->getPlayers().at(1).getScore() << endl;输出
turns 100
turns 100 score 501 score 501预期输出
turns 100 turns 3 score 501 score 11对我可能做错了什么有什么建议吗?
这些只是标准的获取和设置。我相信这套方法是正确的。
获取并设置请求的
int & Player::getTurns()
{
return turns;
}
int &Player::getScore()
{
return score;
}
void Player::setScore(int i)
{
score = i;
}
void Player::setTurns(int i)
{
turns = i;
}播放器类标题
#pragma once
#include <string>
#include <vector>
using namespace std;
class Player
{
private:
int ID;
string name;
//signifies whether this player is human or not (default 0)
bool isHuman = 0;
//this array holds the current turns the AI wants to perform.
//the array is recalculated whenever a new turn takes place IF the AI doesn't get what it expected to hit.
//set calculated pushes one new value onto the array.
//get calulated returns the entire array.
vector<int> calculatedturns;
//a score that starts at 501, and approaches 0 as the GUI progresses.
//The score decreases and the AI calculates a movement path so we need the ability to GET and SET
int score = 501;
//current darts starts at 3 when the round begins, acts like a counter and a trigger, decreases to 0, and then resets back to 3 at the start of a new round.
//the dart number flucuates from 0-3 so we need the ability to GET and SET.
int darts = 3;
//the amount of rounds the GUIs has currently played,
//we need a GET and SET because the GUIs increases, and we need to display them to the user.
int games = 0;
//the amount of rounds the sets has currently played,
//we need a GET and SET because the sets increases, and we need to display them to the user.
int sets = 0;
//the amount of darts the player has currently thrown.
//we need a GET and SET because the throws increases, and we need to display them to the user.
int turns = 100;
//Will determine if the player has actually won the GUI.
//we need to get the current state to determine certain events have taken place, so we need a GET and a SET.
bool has_won = false;
//Will determine if the player has finished the GUI.
//we need to get the current state to determine certain events have taken place, so we need a GET and a SET.
bool has_finished = false;
//the hit accuracy is predefined for a player, and does not change, it requires a GET and SET.
int hit_accuracy = 0;
//hits needs to be incremented and displayed
//we need a GET and SET.
int total_hits = 0;
//misses needs to be incremented and displayed
//we need a GET and SET.
int total_misses = 0;
public:
Player();
/////GETTERS///////
string &getName();
int &getID();
int &getScore();
int &getAccuracy();
int &getDarts();
int &getGames();
int &getSets();
int &getTurns();
bool &getWon();
bool &getFinished();
int &getHits();
int &getMisses();
vector<int> &getCalculated();
/////SETTERS//////
void setName(string s);
void setID(int i);
void setScore(int i);
void setAccuracy(int i);
void setDarts(int i);
void setGames(int i);
void setSets(int i);
void setTurns(int i);
void setWon(bool i);
void setFinished(bool i);
void setHits(int i);
void setMisses(int i);
void setCalculated(int i);
////CONSTRUCTORS////
Player(int ID, string name, int Accuracy);
////METHODS///
//at the start of the game, score is 501.
//AI must find a way to get score to 60.
int Player::AI_CALCULATE_TURN(int currentscore);播放器CPP
#include "Player.h"
//player class is needed because there is more than one player that requires the same information.
//player class holds crucial data about the player, including their current darts, current score, current GUIplan and other things.
Player::Player()
{
}
////GETTERS/////
string & Player::getName()
{
return name;
}
int & Player::getID()
{
return ID;
}
int &Player::getScore()
{
return score;
}
int &Player::getAccuracy()
{
return hit_accuracy;
}
int &Player::getDarts()
{
return darts;
}
int & Player::getGames()
{
return games;
}
int & Player::getSets()
{
return sets;
}
int & Player::getTurns()
{
return turns;
}
bool & Player::getWon()
{
return has_won;
}
bool & Player::getFinished()
{
return has_finished;
}
int & Player::getHits()
{
return total_hits;
}
int & Player::getMisses()
{
return total_misses;
}
vector<int>& Player::getCalculated()
{
return calculatedturns;
}
////SETTERS/////
void Player::setName(string s)
{
name = s;
}
void Player::setID(int i)
{
ID = i;
}
void Player::setScore(int i)
{
score = i;
}
void Player::setAccuracy(int i)
{
hit_accuracy = i;
}
void Player::setDarts(int i)
{
darts = i;
}
void Player::setGames(int i)
{
games = i;
}
void Player::setSets(int i)
{
sets = i;
}
void Player::setTurns(int i)
{
turns = i;
}
void Player::setWon(bool i)
{
has_won = i;
}
void Player::setFinished(bool i)
{
has_finished = i;
}
void Player::setHits(int i)
{
total_hits = i;
}
void Player::setMisses(int i)
{
total_misses = i;
}
void Player::setCalculated(int i)
{
calculatedturns.push_back(i);
}
Player::Player(int id, string Name, int accuracy)
{
name= Name;
ID = id;
hit_accuracy = accuracy;
}
////METHODS
int Player::AI_CALCULATE_TURN(int currentscore)
{
//score is 501, but it could be anything.
//if score is above 60, the AI should be picking a number from the board arrays, it should aim for the higher number possible.
if (currentscore > 60)
{
}
return 0;
}**列出CPP类**
#include "List.h"
//initilizaing the instance.
List* List::theList = NULL;
List * List::getInstance()
{
//will create a new instance if it currently doesn't exist.
if (theList == NULL)
{
theList = new List();
}
return theList;
}
void List::destroyInstance()
{
//deleting the instance
delete theList;
//set the value to null, program won't crash when destructor is called again.
theList = NULL;
}
int List::getExample()
{
return exampleint;
}
vector<Player>List::getPlayers()
{
return PLAYERS_LIST;
}
vector<Player> List::getFinished()
{
return FINISHED_LIST;
}
void List::addPlayer(Player player)
{
PLAYERS_LIST.push_back(player);
}
void List::addFinished(Player player)
{
}
void List::setExample(int i)
{
exampleint = i;
}
List::List()
{
}
List::~List()
{
}列表类头
#pragma once
#include <iostream>
#include <vector>
#include "Player.h"
using namespace std;
//my attempt at a singleton object.
class List
{
public:
//this will create the SINGLE instance
//need to be static because we want to call them if the object doesn't exist
static List* getInstance();
static void destroyInstance();
////getters///
vector<Player> getPlayers();
vector<Player> getFinished();
int getExample();
//basically a setter, pushes a player onto the back of the players list. (to signifiy they're playing)
void addPlayer(Player players);
//basically a setter, pushes a player onto the back of the finished list. (to signify they're finished)
void addFinished(Player player);
void setExample(int i);
private:
//shouldn't be able to call these, prevents the creation of a new instance.
List();
~List();
//the instance itself.
static List* theList;
//players list holds all of the players currently in the game.
vector<Player> PLAYERS_LIST;
//finished list holds the players that have finished the game.
vector<Player> FINISHED_LIST;
int exampleint = 0;
};发布于 2016-04-19 01:49:42
你说的是错的:
vector<Player> List::getPlayers();如果您以下列方式使用它:
d->getPlayers().at(1).setTurns(3); 你实际上是在影响一份副本。您不修改对象中实际持有的球员。
让它返回一个引用:
vector<Player> & List::getPlayers();
// here ^https://stackoverflow.com/questions/36706673
复制相似问题