我正在为C ++游戏做功课,这是我们从CO SCI 136类开始要做的,并且说明中指出:
修改作业4问题1解决方案:
我正在使用Visual Studio 2017,并且遇到以下错误:
Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7
Error C2511 'void Player::setName(const std::string &)': overloaded member function not found in 'Player' player.cpp 18
Error C2597 illegal reference to non-static member 'Player::name' player.cpp 19
有什么办法可以解决这些错误?
这是我的密码
player.h
#pragma once
#include <string>
using namespace std;
class Player
{
private:
string name;
int points;
bool skipturn = false;
public:
Player(const string& new_name = "No Name");
string getName() const;
int getPoints() const;
void setName(string& new_name);
void setPoints(int new_points);
void setLossHalfPoints();
void setSkipTurn(bool isSkip);
bool isSkipTurn();
};
player.cpp
#include <string>
using namespace std;
#include "player.h"
Player::Player(const string& new_name)
{
setName(new_name);
}
string Player::getName() const
{
return name;
}
int Player::getPoints() const
{
return points;
}
void Player::setName(const string& new_name)
{
name = new_name;
}
void Player::setPoints(int new_points)
{
points = new_points;
}
void Player::setLossHalfPoints()
{
this->points /= 2;
}
void Player::setSkipTurn(bool isSkip)
{
this->skipturn = isSkip;
}
bool Player::isSkipTurn()
{
return this->skipturn;
}
source.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <random>
#include<fstream>
#include "player.h"
using namespace std;
int main()
{
int M;
int N;
Player *player; //Declaring as a dynamic array
player = new Player[N];
string *names;
names = new string[N];
ifstream file, file1, file2; //opening the file in read mode
string line;
file.open("Mdata.dat");
file >> M; //Reading the M data from the file
file.close();
file1.open("Ndata.dat");
file1 >> N; //Reading the N data from the file
file1.close();
file2.open("names.dat");
if (file2.is_open()) //if the file is open
{
while (!file2.eof()) //while the end of file is NOT reached
{
getline(file2, line); //get one line from the file
for (int i = 0; i<N; i++)
{
names[i] = line; //reading names from file into names array
}
}
file2.close();
}
for (int i = 0; i < N; i++) //setting the player names from names array
{
player[i].setName(names[i]); player[i].setPoints(0);
}
default_random_engine dre(17890714);
uniform_int_distribution<int> player_uid(0, N - 1);
uniform_int_distribution<int> dice_uid(1, 6);
int index = player_uid(dre);
do
{
index = (index + 1) % N;//implements circular array
if (player[index].isSkipTurn())
{
cout << player[index].getName() << '/' << setw(2) << "skip turn" << endl;
player[index].setSkipTurn(false);// clear skip turn
index = (index + 1) % N;//implements circular array
}
int die1 = dice_uid(dre);
int die2 = dice_uid(dre);
int points = player[index].getPoints();
player[index].setPoints(points + die1 + die2);
if (player[index].getPoints() > M)
{
player[index].setLossHalfPoints();// set half of then points
player[index].setSkipTurn(true);// set skip turn
cout << player[index].getName() << '/' << setw(2) << player[index].getPoints() << '/' << setw(2) << player[index].getPoints() * 2 << endl;
}
else {
cout << player[index].getName() << '/' << setw(2) << die1 + die2 << '/' << setw(2) << player[index].getPoints() << endl;
}
} while (player[index].getPoints() != M);
cout << player[index].getName() << " wins" << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
让我们看看编译器错误。
Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7
有人抱怨说,在player.cpp的第7行调用const std::string
函数时,无法将std::string
转换为setName()
。但是我们在播放器类中有一个setName
函数,对吗?怎么了?
如果您在Player.h中更仔细地查看它,则函数setName
的声明将缺少const
属性。
void setName(string& new_name);
如果您将const
添加到此文件中即可解决该问题。