2013-03-20 85 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()的调用,但它不知道该函数是什么。