潇潇雨雨
它们通常来自忘记包含包含函数声明的头文件,例如,此程序将给出“未声明的标识符”错误:缺少标题int main() {
std::cout <
return 0;}要修复它,我们必须包含标题:#include int main() {
std::cout <
return 0;}如果您编写了标题并正确包含它,标题可能包含错误的包含保护。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx。拼写错误的变量当拼错变量时,会出现另一个常见的初学者错误:int main() {
int aComplicatedName;
AComplicatedName = 1; /* mind the uppercase A */
return 0;}范围不正确例如,此代码会出错,因为您需要使用std::string:#include int main() {
std::string s1 = "Hello"; // Correct.
string s2 = "world"; // WRONG - would give error.}申报前使用void f() { g(); }void g() { }g在首次使用之前尚未申报。要修复它,要么移动g之前的定义f:void g() { }void f() { g(); }或者在g之前添加声明f:void g(); // declarationvoid f() { g(); }void g() { } // definitionstdafx.h不在顶部(VS特定)这是特定于Visual Studio的。在VS中,您需要#include "stdafx.h"在任何代码之前添加。编译器忽略它之前的代码,所以如果你有这个:#include #include "stdafx.h"在#include 将被忽略。你需要将它移到下面:#include "stdafx.h"#include 随意编辑这个答案。