【问题标题】:error C3861: 'rollDice': identifier not found错误 C3861:“rollDice”:未找到标识符
【发布时间】:2013-04-23 20:07:41
【问题描述】:

我正在尝试实现一些图形,但在调用最底部显示的函数 int rollDice() 时遇到问题,并且不知道如何解决这个问题?任何想法...我收到一个错误 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;
} 

更新

【问题讨论】:

标签: c++ visual-studio-2010 visual-studio-2012 mfc


【解决方案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()
{
    ...
}

还要注意函数原型是由name指定的,还有返回值参数

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

这就是区分函数的方式int foo();void foo(); 是不同的函数,但是由于它们仅在返回值上有所不同,因此它们不能存在于同一范围内(有关更多信息,请参见 Function Overloading)。

【讨论】:

    【解决方案2】:

    把函数声明rollDice

     int rollDice();
    

    OnBnClickedButton1 之前,或者只是将rollDice 函数的定义移到OnBnClickedButton1 之前。

    原因是在你当前的代码中,当你在OnBnClickedButton1 中调用rollDice 时,编译器还没有看到这个函数,这就是你看到identifier not found 错误的原因。

    【讨论】:

    • 得到了同样的错误,因为被调用的函数被放置在调用函数之后。更改顺序修复了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多