错误C3861:"rollDice":找不到标识符

编程入门 行业动态 更新时间:2024-10-22 08:29:17
本文介绍了错误C3861:"rollDice":找不到标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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":找不到标识符

本文发布于:2023-10-07 15:22:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469757.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标识符   找不到   错误   quot   rollDice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!