我的21场比赛有骰子的C ++错误请帮忙

时间:2015-11-24 17:45:32

标签: c++

这是我的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 DiceTwentyOne :: playGame(bool)”中引用( ?playGame @ DiceTwentyOne @@ QAEH_N @ Z)C:\ Users \ willr_000 \ Documents \ Visual Studio 2012 \ Projects \ 21场比赛\一场比赛21 \ DiceTwentyOne.obj 21场比赛

1 个答案:

答案 0 :(得分:2)

您使用playGame()致电bool,但定义为playGame(std::string)