Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c+;中带有动态数组的简单竞赛游戏+; 我在做一个C++游戏的作业,我们必须从Co SCI 136类中做,并且指令说明:_C++_Visual Studio 2017_Dynamic Arrays - Fatal编程技术网

c+;中带有动态数组的简单竞赛游戏+; 我在做一个C++游戏的作业,我们必须从Co SCI 136类中做,并且指令说明:

c+;中带有动态数组的简单竞赛游戏+; 我在做一个C++游戏的作业,我们必须从Co SCI 136类中做,并且指令说明:,c++,visual-studio-2017,dynamic-arrays,C++,Visual Studio 2017,Dynamic Arrays,修改作业4问题1的解决方案,从而: 用动态数组替换该数组 从文件中读取获胜分数M 从文件中读取玩家数N 从文件中读取播放器名称 我正在使用Visual Studio 2017,我遇到了以下错误: Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7

修改作业4问题1的解决方案,从而:

  • 用动态数组替换该数组
  • 从文件中读取获胜分数M
  • 从文件中读取玩家数N
  • 从文件中读取播放器名称
  • 我正在使用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();
    };
    
    #pragma一次
    #包括
    使用名称空间std;
    职业选手
    {
    私人:
    字符串名;
    积分;
    bool skipturn=false;
    公众:
    播放器(const string&new_name=“No name”);
    字符串getName()常量;
    int getPoints()常量;
    无效集合名(字符串和新集合名);
    无效设定点(int新设定点);
    void setLossHalfPoints();
    无效设置基普图恩(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;
    }
    
    #包括
    使用名称空间std;
    #包括“player.h”
    播放器::播放器(常量字符串和新名称)
    {
    设置名称(新名称);
    }
    字符串播放器::getName()常量
    {
    返回名称;
    }
    int Player::getPoints()常量
    {
    返回点;
    }
    void Player::setName(常量字符串和新名称)
    {
    名称=新名称;
    }
    无效播放器::设置点(int新点)
    {
    点数=新点数;
    }
    void Player::setLossHalfPoints()
    {
    该->点/=2;
    }
    无效玩家::setSkipTurn(bool-isSkip)
    {
    此->skipTun=isSkip;
    }
    布尔播放器::isSkipTurn()
    {
    返回此->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;
    }
    
    #包括“stdafx.h”
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括“player.h”
    使用名称空间std;
    int main()
    {
    int M;
    int N;
    Player*Player;//声明为动态数组
    玩家=新玩家[N];
    字符串*名称;
    名称=新字符串[N];
    ifstream file,file1,file2;//以读取模式打开文件
    弦线;
    打开文件(“Mdata.dat”);
    file>>M;//从文件中读取M数据
    file.close();
    文件1.打开(“数据数据”);
    file1>>N;//从文件中读取N个数据
    file1.close();
    文件2.打开(“names.dat”);
    if(file2.is_open())//如果文件已打开
    {
    while(!file2.eof())//未到达文件结尾时
    {
    getline(file2,line);//从文件中获取一行
    
    for(int i=0;i让我们看看编译器错误

    Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7
    
    它抱怨在调用player.cpp第7行的
    setName()
    函数时无法将
    const std::string
    转换为
    std::string
    。但是我们在player类中有一个
    setName
    函数,对吗?怎么了

    如果在Player.h中仔细查看,函数
    setName
    的声明缺少
    const
    属性

    void setName(string& new_name);
    

    如果你添加
    const
    ,这将解决问题。

    我不是想说得粗鲁,但编译器的错误告诉了你确切的问题。你不明白他们是怎么回事吗?请给出显示内容的文件名,并指出错误消息中提到的行。添加
    //这行错误1/2/3
    为此,您声明了
    void setName(string和new_name);
    但定义了
    void Player::setName(const string和new_name)
    const
    表示它们是不同的函数:
    bool isSkipTurn()
    应该是
    bool isSkipTurn()const <代码>请不要使用代码段。它们是HTML/CSS/JavaScript,而不是C++。谢谢。我现在得到了。它修复了错误,但是现在它有:运行时检查失败3。变量n’被使用而不被初始化。occurred@Luis查看您的代码。首先声明
    N
    ,然后使用
    N
    ,然后阅读
    N
    。这是错误的顺序,它应该是declare
    N
    ,read
    N
    ,use
    N
    。现在再想想错误消息。它现在有意义吗?在给变量赋值之前不能使用它。@john我现在明白了。我只是声明int N=0;@Luis No,因为这会给
    N
    一个值,所以错误会变为a但是你给了它一个错误的值,正确的值不是零,而是你从文件中读取的值。程序中发生的事情的顺序很重要,特别是在你在其他计算中使用这些变量之前,你必须给变量你想要的值,所以
    file1>>N;
    必须在
    player=ne之前w Player[N];
    names=new string[N];
    不在之后。@Luis您的程序是一系列指令,它们按照给定的顺序执行。您所拥有的就像一份食谱,上面写着:1)将糖和黄油混合,2)称出1/4磅糖。顺序是从前到后的。