C++ 错误 C3861:“rollDice”:未找到标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16290834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C3861: 'rollDice': identifier not found
提问by Mac
I am trying implement some graphics, but I am having trouble calling the function int rollDice() shown on the very bottom and am not sure how to solve this? any ideas... I am getting an error error C3861: 'rollDice': identifier not found.
我正在尝试实现一些图形,但我在调用最底部显示的函数 int rollDice() 时遇到问题,并且不确定如何解决这个问题?任何想法...我收到一个错误错误 C3861: 'rollDice': identifier not found。
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;
}
updated
更新
回答by LihO
Compiler goes through your files from the beginning till the end, meaning that the placement of the definition of your function matters. In this case, you can either move the definition of this function before it is used first time:
编译器从头到尾遍历您的文件,这意味着函数定义的位置很重要。在这种情况下,您可以在第一次使用此函数之前移动它的定义:
void rollDice()
{
...
}
void otherFunction()
{
// rollDice has been previously defined:
rollDice();
}
or you can use forward declarationto tell the compiler that such a function exists:
或者您可以使用前向声明告诉编译器存在这样的函数:
// function rollDice with the following prototype exists:
void rollDice();
void otherFunction()
{
// rollDice has been previously declared:
rollDice();
}
// definition of rollDice:
void rollDice()
{
...
}
Also note that function prototype is specified by name, but also return valueand parameters:
另请注意,函数原型由name指定,但也有返回值和参数:
void foo();
int foo(int);
int foo(int, int);
this is how functions are being distinguished. int foo();
and void foo();
are different functions, however since they differ only in their return value, they can not exist within the same scope (for more info see Function Overloading).
这就是功能的区分方式。int foo();
和void foo();
是不同的函数,但是由于它们仅在返回值上有所不同,因此它们不能存在于同一范围内(有关更多信息,请参阅函数重载)。
回答by taocp
Put declaration of the function rollDice
put 函数的声明 rollDice
int rollDice();
before OnBnClickedButton1
or simply move the definition of the rollDice
function before OnBnClickedButton1
.
beforeOnBnClickedButton1
或简单地将rollDice
函数的定义移到before OnBnClickedButton1
。
The reason is in your current code when you call rollDice
inside OnBnClickedButton1
, the function has not been seen by the compiler yet, that's why you saw that identifier not found
error.
原因是在您当前的代码中,当您调用rollDice
inside 时OnBnClickedButton1
,编译器尚未看到该函数,这就是您看到该identifier not found
错误的原因。