由于值返回函数中的错误,程序无法编译.它说未声明的标识符

Program not compiling due to error in the value returning function. It says undeclared identifier

本文关键字:编译 标识符 未声明 返回 于值 函数 错误 程序      更新时间:2023-10-16

我正在为石头剪刀布编写一个程序,其中有一个用户播放器和一个计算机播放器。我相信一切都很好,直到布尔值返回函数。它需要两个参数(计算机的选择,玩家的选择(,看看它们是否等于打印出"平局"。但是,我收到一个错误,指出我的两个参数的未声明标识符。

我尝试将其更改为 int 函数而不是 bool。 并将我的布尔语句放在 main 中,但它不起作用

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int getComputerChoice();
int getPlayerChoice();
bool isTie (int, int);
int main()
{
char choice;
int compChoice;
int plaChoice;

do
{
cout << "ROCK PAPER SCISSORS MENU" << endl;
cout << "--------------------------" << endl;
cout << "p)Play Game" << endl;
cout << "q)Quit" << endl;
cout << "Please enter your choice : " << endl;
cin >> choice;

while (choice != 'p' && choice != 'q')//or if//why &&
{
cout << "Invalid selection. Try again." << endl << endl << endl;
cin >> choice;
}
switch (choice)
{
case 'p':
compChoice = getComputerChoice();
plaChoice = getPlayerChoice();
if (plaChoice == 1)
{
cout << "You chose: Rock" << endl;
}
else if (plaChoice == 2)
{
cout << "You chose: Paper" << endl;
}
else 
{
cout << "You chose: Scissors" << endl;
}

if (compChoice == 1)
{
cout << "The computer chose: Rock" << endl;
}
else if (compChoice == 2)
{
cout << "The computer chose: Paper" << endl;
}
else
{
cout << "The computer chose: Scissors" << endl;
}

if (isTie(compChoice, plaChoice))
{
cout << "It is a Tie!";
}

break;

case 'q':
cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
break;
}
} while (choice != 'q');

return 0;
}

int getComputerChoice()
{
int comp = 0;
int rando = 0;
srand((unsigned int)time(NULL));
rando = rand() % 3 + 1;
switch (rando)
{
case 1:
comp = 1;
break;
case 2:
comp = 2;
break;
case 3:
comp= 3;
break;
return comp;
}
}

int getPlayerChoice()
{
int player;
cout << "Rock, Paper or Scissors?" << endl;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "Please enter your choice: " << endl;
cin >> player;
while (player != 1 && player != 2 && player != 3)
{
cout << "Invalid" << endl;
cin >> player;
}
return player;
}
bool isTie(compu, playa)
{
if (compu == playa)
return true;
else
return false;
}

这些是我收到的错误消息 compu':未声明的标识符 Playa':未声明的标识符 "isTie":重新定义;以前的定义是"功能" 参见"isTie"声明 'isTie':函数样式初始值设定项似乎是函数定义

isTie

是一个有2个参数的函数。 从您的代码中,我可以看到它期待 2integers.

因此,您需要将函数签名更新为:

bool isTie(int compu, int playa)