简体   繁体   中英

simple race game with dynamic array in c++

I was doing homework for a C++ game that we have to do from CO SCI 136 class and the instructions states:

Modify your Homework 4 Problem 1 solution thus:

  1. Replace the array with a dynamic array
  2. Read the winning points M from a file
  3. Read the number of players N from a file
  4. Read the player names from a file.

I am using Visual Studio 2017 and I am having trobule with these errors:

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  

Is there any way to fix these errors?

Here are my codes of

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;
}

Let's look at the compiler error.

Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7

It is complaining that it cannot convert const std::string to std::string when calling the setName() function at line 7 of player.cpp. But we have a setName function in the player class, right? What's wrong?

If you look at it more carefully in your Player.h, the declaration of the function setName is missing the const attribute.

void setName(string& new_name);

If you add const to this that will solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM