我可以为您提供中文翻译服务,以下是需要翻译的内容:
现在,用户拥有这些属性的列表。
这是一个非常简单的空间类,正如您所要求的。
我正在编写一个大富翁游戏。我非常喜欢使用排序函数来生成漂亮干净的输出,例如按组对玩家拥有的财产进行排序。
首先,这是我的财产排序函数:
bool propertySort(Property a, Property b) { return a.getGroup() < b.getGroup(); }
现在,用户拥有这些属性的列表。
#pragma once
#include <string>
#include <vector>
#include "Dice.h"
#include "Property.h"
class Player
{
public:
...
private:
int curMoney{ 1500 };
int curLocation{ 0 };
int playerNum{ 0 };
std::string name;
std::vector<Property> ownedProperties{};
int curDiceRoll;
};
正如您所看到的,它是一个私有属性,向下初始化为0个属性。
因此,在我的主要属性排序器中,我将显示属性到板上供玩家查看,但排序函数不断给我提供错误C2280。我知道这正是排序函数引发此错误,因为如果我注释掉排序行,程序就可以正常运行。我确定这是我作为初学者程序员所不知道的显而易见的东西。如果有人能提供关于这是什么的见解,那就太好了,谢谢!
void viewProperties(Player& p)
{
string prompt = "Select a player to view their properties";
vector<string> playerNames{};
for (Player p : players)
{
playerNames.push_back(p.getName());
}
Menu propertiesMenuSelection{ prompt, playerNames };
vector<Property> propertiesOfSelectedPlayer = players[propertiesMenuSelection.getChoice()].getOwnedProperties();
sort(propertiesOfSelectedPlayer.begin(), propertiesOfSelectedPlayer.end(), propertySort);
system("CLS");
如果有帮助的话,这里是Property类
#pragma once
#include "Space.h"
#include <array>
#include <vector>
extern std::vector<Player> players;
class Property : public Space
{
public:
Property(std::string name, std::vector<int> values);
~Property();
void run(Player&) override;
std::vector<std::string> boardOut() const override;
std::string getName() const override;
...
private:
std::string name;
int group;
int price;
int buildingCost;
int buildings;
Player* ownerPtr = nullptr;
std::array <int, 6> rent;
const std::array <std::string, 10> groups{ "Brown", "Light Blue", "Pink", "Orange", "Red", "Yellow", "Green", "Dark Blue", "Railroad", "Utility" };
};
这是一个非常简单的空间类,正如您所要求的。
#pragma once
#include <string>
#include <vector>
class Player;
class Space
{
public:
virtual void run(Player&) = 0;
virtual std::string getName() const = 0;
virtual std::vector<std::string> boardOut() const = 0;
};
const std::array
- 这个成员变量可能是为什么没有可用的复制功能。 - PaulMcKenzieconst
变量赋值。愚蠢的例子:https://ideone.com/AieNNX - user4581301const
的。这是一篇旧文,但与此相关的文章,来自Dr. Dobbs - PaulMcKenzie