error C3861: 'rollDice': identifier not found

49,727

Solution 1

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

Also note that function prototype is specified by name, but also return value and parameters:

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).

Solution 2

Put declaration of the function rollDice

 int rollDice();

before OnBnClickedButton1 or simply move the definition of the rollDice function 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.

Share:
49,727
Mac
Author by

Mac

Updated on March 21, 2020

Comments

  • Mac
    Mac about 4 years

    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;
    } 
    

    updated

  • Broken_Window
    Broken_Window over 5 years
    Got the same error because the called function was placed after the calling function. Changing the order fixed it.