1

我正在为我的编程课程开发一个项目,当我尝试编译它时,我不断收到构建错误 LNK2019。我曾尝试咨询另一位 CPS 专业,但他不太确定我为什么不断收到错误消息。

// File: Asgn7_Skel.cpp
// CPS 150/Fall 2013 Assignment 7 Skeleton
// Modified from Gaddis Program 8-32 to satisfy the assignment
// requirements.
// Skeleton by: Tamisra H. Sanyal
// Completed by: Kyle Abel
// This program lets the user play a game of rock, paper, scissors
// with the computer. The computer's choices are randomly generated.
// Total 10 rounds are played. 
// Change the NumRounds constant to play a different number of rounds

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <string>
#include <array>
using namespace std;

// enumerations of choices and outcomes
enum Choices {Rock, Paper, Scissors, Last};
enum Outcomes {ComputerWin, PlayerWin, Tie,};

// Data for one round
struct GameData 
{
    Choices computerChoice;
    Choices playerChoice;
    Outcomes result;
}; // end struct

const int NumRounds(10);    // number of rounds to play
const int ArraySize(NumRounds); // we will not use index 0

typedef array<GameData, ArraySize> DataStore;

// These constant arrays are used to translate enumeration constants to
// readable descriptions (see line 9 in textbook's program)
const array<string, 3> ChoiceNames = {"Rock", "Paper", "Scissors"};
const array<string, 3> OutcomeNames = {"Computer win", "Player win", "Tie"};
array<int, 3> ResultsTalley = {};

// Function prototypes
void getPlayerChoice(Choices & choice);
Outcomes getRoundResult(const Choices computerChoice, const Choices playerChoice);

void showRoundResult(const Choices computerChoice, const Choices playerChoice,
const Outcomes roundResult);

void showGameResults(const DataStore & results);

int main() 
{
    Choices computerChoice, playerChoice;
    Outcomes roundResult;
    int computerPoints = 0, playerPoints = 0;  // Point accumulators
    DataStore gameResults = {};

    srand(time(NULL));  // Give the random generator

    int counter;       // a seed to start with 
    int pl_choice;

    cout << "CPS 150 Assignment 7 by Kyle Abel\n\n";
    cout << "Let's play Rock-Paper-Scissors!\n";
    cout << "We will play " << NumRounds << " rounds.\n\n";

    // TODO: Add main function code here
    for (counter = 0; counter < NumRounds; counter++)
    {
        computerChoice = static_cast<Choices>(rand() % 3);  
        getPlayerChoice(playerChoice);  
        getRoundResult(computerChoice, playerChoice);   
        showRoundResult(computerChoice, playerChoice, roundResult);

        if (Tie)
        {
            return 0;
        }
        else if (ComputerWin)
        {
            computerPoints++;
        }
        else 
            playerPoints++;
    } // end for

    cout << "\nCPS 150 Assignment 7 complete\n\n";
    return 0;
} // end main

void showGameResults(const DataStore & results) 
{
    cout << "Game results\n";
    cout << "============\n";
    cout << right << setw(5) << "Round" << ' ' 
        << left << setw(14) << "Player choice"
        << setw(16) << "Computer choice" 
        << setw(13) <<"Round result" << endl;
    cout << right << setw(5) << "-----" << ' ' 
        << left << setw(14) << "-------------"
        << setw(16) << "---------------" 
        << setw(13) <<"------------" << endl;

    for (int k(1); k < results.size(); k++) 
    {
        cout << right << setw(5) << k << ' ' << left
            << setw(14) << ChoiceNames[results[k].playerChoice] 
            << setw(16) << ChoiceNames[results[k].computerChoice] 
            << setw(13) << OutcomeNames[results[k].result] << endl;
    } // end for

    cout << endl;
} // end showGameResults

void getPlayerChoice(Choices playerChoice, int PChoice) 
{
    cout << "Enter your choice, 1 for Rock, 2 for Paper, or 3 for Scissors";
    cin >> PChoice;

    if (PChoice != 1 || PChoice != 2 || PChoice != 3) 
    {
        cout << "Please Enter a Valid # 1-3" << "\n\n";
        cin >> PChoice;
    }
    else if (PChoice = 1) 
    {
        PChoice--;
        static_cast<Choices>(PChoice);          
    }
    else if (PChoice = 2) 
    {
        PChoice--;
        static_cast<Choices>(PChoice);      
    }
    else 
    {
        PChoice--;
        static_cast<Choices>(PChoice);       
    }
}

Outcomes getRoundResult(const Choices computerChoice, const Choices playerChoice)
{
    if (computerChoice == playerChoice) 
    {
        return Tie;
    }
    else if ((playerChoice == 1 && computerChoice == 2) ||
            (playerChoice == 2 && computerChoice == 3) ||
            (playerChoice == 3 && computerChoice == 1) )
    {
        return ComputerWin;
    }
    else 
    {
        return PlayerWin;
    }
}

void showRoundResult(const Choices computerChoice, const Choices playerChoice, const Outcomes roundResult)
{ 
    if (Outcomes(Tie))
    {
       cout << "we have tied!";
    }
    else if (Outcomes(ComputerWin))
    {
       cout << "I chose " << ChoiceNames[computerChoice] << ", so I win the game! "
            << ChoiceNames[computerChoice] << " beats " << ChoiceNames[playerChoice]
            << ". \n\n";                
    }
    else if (Outcomes(PlayerWin))
    {
       cout << "I chose " << ChoiceNames[computerChoice] << ", so you won!! "
            << ChoiceNames[playerChoice] << " beats " << ChoiceNames[computerChoice]
            << ". \n\n";
    }
}

这些是我在尝试构建项目时不断遇到的错误:

1>------ Build started: Project: ConsoleApplication15, Configuration: Debug Win32 ------
1>Source.obj : error LNK2019: unresolved external symbol "void __cdecl     getPlayerChoice(enum Choices &)" (?getPlayerChoice@@YAXAAW4Choices@@@Z) referenced in   function _main
1>C:\Users\Kyle\documents\visual studio 2012\Projects\ConsoleApplication15\Debug\ConsoleApplication15.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何有关如何解决此问题的指导将不胜感激!

4

3 回答 3

4

出现此问题是因为您的函数原型与函数定义不同。您的原型为:

void getPlayerChoice(Choices & choice);

但是您对该功能的定义是:

void getPlayerChoice(Choices playerChoice, int PChoice)

要解决此问题,函数原型和定义必须相同。

于 2013-11-14T19:40:54.087 回答
1

好吧 - 你没有定义getPlayerChoice(Choices & choice)函数,所以编译器会给你一个错误。

你的代码中有一个声明getPlayerChoice(Choices & choice)定义。void getPlayerChoice(Choices playerChoice, int PChoice)那不一样。您调用 main 中的第一个,虽然它没有定义,因为您定义了它的不同版本。

于 2013-11-14T19:40:05.913 回答
0

我会重写您的 getPlayerChoice 函数以使用循环,因为如果用户输入两个无效的选择,它将通过。这就是我的想法。

void getPlayerChoice(Choices& playerChoice, int PChoice) {
    cout << "Enter your choice, 1 for Rock, 2 for Paper, or 3 for Scissors";
    cin >> PChoice;

    while (PChoice != 1 || PChoice != 2 || PChoice != 3) {
        cout << "Please Enter a Valid # 1-3" << "\n\n";
        cin >> PChoice;
    }

    //Now that we're sure it's either 1 2 or 3, we can alter the rest of the code a bit
    PChoice--;            //Instead of once in each branch
    playerChoice = static_cast<Choices>(PChoice); //Why cast it without assigning it?
}

我刚刚有了另一个想法,而且,我担心我可能已经完成了太多你的任务,我不会包含代码,但如果你弄清楚了,你会真的给你的教授留下深刻的印象(哦,太好了,现在我'''''''''''''''''''''''''''''''''''''Heinz Doofenschmirtz''''''''''''''''''''''''''''''''''''''' 个让人万种,还能在我脑海中唱“我必须给我的教授留下深刻印象”的声音)。获胜者可以通过将用户的选择转换为整数,减去计算机的选择转换为整数,然后对结果使用模数 3 来确定。0=平局,1=人类,2=PC。

于 2013-11-18T14:42:19.623 回答