2013-04-30 57 views
8

我想实现一些图形,但我有麻烦调用函数int rollDice()显示在最底部,我不知道如何解决这个问题?任何想法...我得到一个错误错误C3861:'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; 
} 

更新功能rollDice

int rollDice(); 

+3

采取相关链接到正确的,http://stackoverflow.com/questions/12723107/error-c3861-initnode-identifier-not-found?rq=1 – chris 2013-04-30 01:32:29

+1

为什么你编辑你的问题,包括答案?这个问题现在没有意义。 – caps 2016-08-11 19:57:50

回答

25

编译器会从头到尾查看您的文件,这意味着您的函数定义的位置很重要。在这种情况下,你可以移动这个函数的定义,使用前第一次:

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();是不同的功能,但由于它们仅在返回值上有所不同,所以它们不能在同一范围内存在(更多信息,请参阅Function Overloading)。

+0

我的项目没有工作,直到我改变'无效'的顺序。谢谢!!! +1。 – 2013-09-27 17:10:29

2

认沽声明OnBnClickedButton1之前或者干脆OnBnClickedButton1之前移动rollDice函数的定义。

原因是在您当前的代码中调用rollDiceOnBnClickedButton1,编译器还没有看到函数,这就是为什么您看到identifier not found错误。

相关问题