2013-03-20 84 views
0

這裏當未找到我的代碼:C++標識符編譯源

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <math.h> 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int userInput = -9999; 
    userInput = ReadNumber(); 
    WriteAnswer(userInput); 
    system("pause"); 
    return 0; 
}; 

int ReadNumber() 
{ 
    int liInput = -9999; 
    cin >> liInput; 
    return liInput; 
}; 

void WriteAnswer(int data) 
{ 
    cout << data << endl; 
}; 

當我試圖編譯,它甾體抗炎藥:

1>錯誤C3861: 'ReadNumber':標識符找不到

1> error C3861:'WriteAnswer':標識符未找到

爲什麼會出現上述錯誤?以及如何解決這個問題?

謝謝

回答

5

C++源代碼從頭到尾編譯爲

當編譯器這一步得到:

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <math.h> 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int userInput = -9999; 
    userInput = ReadNumber(); // <-- What is this? 

這是真的 - 沒有現有的ReadNumber證據。

在使用之前聲明你的函數的存在。

int ReadNumber(); 
void WriteAnswer(int data); 
2

你忘了輸入函數原型。

int ReadNumber (void); 
void WriteAnswer(int); 

在調用函數之前將它們放入代碼中。

+0

哦,是的,我忘了 – User2012384 2013-03-20 15:22:55

1

在您的代碼中,您嘗試調用ReadNumber函數,該函數尚未聲明。編譯器不知道任何關於此功能:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ... 
    ReadNumber(); // call to function ReadNumber, but what is ReadNumber ??? 
} 

// definition of ReadNumber: 
int ReadNumber() 
{ 
    ... 
} 

你應該首先聲明它:

// declaration: 
int ReadNumber(); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ... 
    ReadNumber(); // call ReadNumber that takes no arguments and returns int 
} 

// definition of ReadNumber: 
int ReadNumber() 
{ 
    ... 
} 
0

必須寫一個函數原型或函數的第一次調用之前函數本身。

在您的代碼編譯器中,請參閱ReadNumber()的調用,但它不知道該函數是什麼。