我的21场骰子游戏的C++错误,请帮忙

C++ error for my 21 game with dice help please

本文关键字:错误 C++ 21场 游戏 我的      更新时间:2023-10-16

这是我的main.cpp

#include "DiceTwentyOne.h"
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;
DiceTwentyOne game;
string play;
void playGame(string play)
{
    int turns = 0;
    while(play == "yes" && turns !=3)
    {
        cout << "Your score is: " << game.playGame(true);
        cout << endl;
        cout << "Would you like to roll?[yes,no]";
        cin >> play;
        cout << endl;
        turns++;
    }
    cout << "Dealers score: " << game.playGame(false) << endl;
    if(game.winLoss())
    {
        cout << "You win";
    }
    else
    {
        cout << "Dealer wins";
    }
}
int main ()
{
    cout << "Would you like to roll?[yes,no]";
    cin >> play;
    cout << endl;
    if(play == "yes")
    {
        playGame(play);
    }
    else
    {
        cout << "Have a nice day.";
    }
    getch();
    return 0;
}

die.h

#ifndef DIE.H
#define DIE.H
#include<stdlib.h> 
#include<time.h> 
#include<iostream>
using namespace std;
class Die
{
public:
    Die(); 
    void changeFace(); 
    int getFace(); 
private:
    int faceValue; 
    void rollDie(); 
};
#endif

die.cpp

#include "Die.h"
Die::Die()
{
    faceValue; 
}
void Die::rollDie()
{
    int temp= (unsigned) time(0);
    srand(rand()%temp*((unsigned) time (NULL)
        *(unsigned)time(NULL)));
        faceValue = (rand()%((6-1)+1)+1);
}
void Die::changeFace()
{
    rollDie();
}
int Die::getFace()
{
    return faceValue;
}

player.h

#ifndef PLAYER.H
#define PLAYER.H
class Player
{
private:
    int turns, score;
public:
    Player();
    void setScore(int points);
    int getScore();
    void setTurns(int turns);
    int getTurns();
};
#endif

player.cpp

#include "Player.h"
Player::Player()
{
    score = 0;
    turns = 0;
}
void Player::setScore(int points)
{
    score = points + score;
}
int Player::getScore()
{
    return score;
}
void Player::setTurns(int turn)
{
    turns += turn;
}

dicetwentyone.h

#ifndef DICETWENTYONE.H
#define DICETWENTYONe.H
#include "Player.h"
#include "Die.h"
class DiceTwentyOne
{
private:
    Player player;
    Player dealer;
    Die die;
public:
    DiceTwentyOne();
    int playGame(bool truth);
    bool winLoss();
};
#endif

dicetwentyone.cpp

#include "DiceTwentyOne.h"
#include "Player.h" 
DiceTwentyOne::DiceTwentyOne()
{
    player;
    dealer;
    die;
}
int DiceTwentyOne::playGame(bool truth)
{
    player.setTurns(1);
    if(player.getScore()<21)
    {
        if(player.getTurns()<=3 && truth)
        {
            for(int i=0; i<2; i++)
            {
                die.changeFace(); 
                player.setScore(die.getFace()); 
            }
            return player.getScore();
        }
        else
        {
            while(dealer.getScore()<15
                && dealer.getTurns()<=3)
            {
                dealer.setTurns(1);
                for(int i=0; i<2; i++)
                {
                    die.changeFace();
                    dealer.setScore(die.getFace());
                }
            }
            int tempTurn = -1 * dealer.getTurns();
            dealer.setTurns(tempTurn);
            tempTurn = -1 * player.getTurns();
            player.setTurns(tempTurn);
            return dealer.getScore();
        }
    }
    return -1;
}
bool DiceTwentyOne::winLoss()
{
    if(dealer.getScore > 21)
    {
        return false;
    }
    else if(player.getScore() > dealer.getScore())
    {
        return true;
    }
    else
    {
        return false;
    }
}

我收到了这个错误,我已经尝试为getTurns赋值。有人知道如何解决这个问题吗?

错误1错误LNK2019:未解析的外部符号"public:int__thiscall Player::getTurns(void)"(?getTurns@Player@@QAEHXZ)在函数"public:int__thiscall DiceTwentOne::playGame(bool)"(?playGame@DiceTwentyOne@@QAEH_N@Z)C:\Users\will_000\Documents\Visual Studio 2012\Projects\ 21的游戏\ 21的游戏\DiceWentyOne.obj 21 的游戏

使用bool调用playGame(),但它被定义为playGame(std::string)