错误C3861:“rollDice”:标识符未找到(error C3861: 'rollDi

2019-09-01 10:12发布

我想实现一些图形,但我有在最底部所示的麻烦调用函数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;
} 

更新

Answer 1:

编译器经过从开始到结束你的文件,这意味着你的函数事项的定义的位置。 在这种情况下,你可以移动这个函数的定义,它是用来首次使用前:

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()
{
    ...
}

还要注意的是函数的原型是由名称指定,而且还返回值参数

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

这是功能是如何被区分int foo();void foo(); 有不同的功能,但是因为他们在返回值只有不同,但它们不能在同一范围内的存在(更多信息请参见函数重载 )。



Answer 2:

该函数的声明放在rollDice

 int rollDice();

OnBnClickedButton1或只需将定义rollDice之前功能OnBnClickedButton1

原因是在当前的代码,当你调用rollDiceOnBnClickedButton1 ,该功能尚未由编译器见过的,这就是为什么你看到identifier not found的错误。



文章来源: error C3861: 'rollDice': identifier not found