gpt4 book ai didi

c++ - 错误 C3861 : 'rollDice' : identifier not found

转载 作者:可可西里 更新时间:2023-11-01 16:57:30 26 4
gpt4 key购买 nike

我正在尝试实现一些图形,但我在调用最底部显示的函数 int rollDice() 时遇到问题,我不确定如何解决这个问题?任何想法...我收到错误错误 C3861:“rollDice”:找不到标识符。

int rollDice();

void CMFCApplication11Dlg::OnBnClickedButton1()
{

enum Status { CONTINUE, WON, LOST };
int myPoint;
Status gameStatus;
srand( (unsigned)time( NULL ) );
int sumOfDice = rollDice();

switch ( sumOfDice )
{
case 7:
case 11:
gameStatus = WON;
break;

case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
break;
}
while ( gameStatus == CONTINUE )
{
rollCounter++;
sumOfDice = rollDice();

if ( sumOfDice == myPoint )
gameStatus = WON;
else
if ( sumOfDice == 7 )
gameStatus = LOST;
}


if ( gameStatus == WON )
{

}
else
{

}
}

int rollDice()
{
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
return sum;
}

已更新

最佳答案

编译器从头到尾遍历文件,这意味着函数定义的位置很重要。在这种情况下,您可以在第一次使用之前移动此函数的定义:

void rollDice()
{
...
}

void otherFunction()
{
// rollDice has been previously defined:
rollDice();
}

或者您可以使用前向声明告诉编译器存在这样的函数:

// function rollDice with the following prototype exists:
void rollDice();

void otherFunction()
{
// rollDice has been previously declared:
rollDice();
}

// definition of rollDice:
void rollDice()
{
...
}

另请注意,函数原型(prototype)由名称返回值参数指定:

void foo();
int foo(int);
int foo(int, int);

函数就是这样区分的。 int foo();void foo(); 是不同的函数,但是由于它们只在返回值上不同,所以它们不能存在于同一范围内(更多信息参见 Function Overloading )。

关于c++ - 错误 C3861 : 'rollDice' : identifier not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com