error C3861: #39;rollDice#39;: identifier not found(错误 C3861:“rollDice:未找到标识符)
问题描述
我正在尝试实现一些图形,但在调用最底部显示的函数 int rollDice() 时遇到问题,并且不知道如何解决这个问题?任何想法...我收到一个错误 error C3861: 'rollDice': identifier not found.
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();
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;
}
更新
推荐答案
编译器从头到尾遍历文件,这意味着函数定义的位置很重要.在这种情况下,您可以在第一次使用此函数之前移动它的定义:
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 declaration to 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()
{
...
}
还要注意函数原型是由name指定的,还有返回值和参数:
Also note that function prototype is specified by name, but also return value and parameters:
void foo();
int foo(int);
int foo(int, int);
这就是区分函数的方式.int foo();
和 void foo();
是不同的函数,但由于它们的区别仅在于返回值,因此它们不能存在于同一范围内(更多信息参见函数重载).
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).
这篇关于错误 C3861:“rollDice":未找到标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误 C3861:“rollDice":未找到标识符
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01