C++常见错误及编译错误汇集

简介:
C++课程上机实验常见错误汇集
1.  在源码中遗失“;”
调试器错误信息:syntax error : missing ';'
2.  缺少命名空间使用定义:即缺少“using namespace std;”
调试器错误信息:例:error C2065: 'cout' : undeclared identifier
例如cout/cin/endl/<</>>等在命名空间中定义的符号和标示符无法使用。
3.  变量未定义就直接使用
调试器错误信息:例:error C2065: 'i' : undeclared identifier
       C++语言中,变量的使用必需遵循先声明定义,后使用的原则。
4.  在程序中使用中文标示符,如将英文”;”错误输入成了”;”
调试器错误信息:error C2018: unknown character '0xa3'
在C++中,除程序注释可以采用中文外,其余字符要求使用英文。不少同学在建立工程或程序名称时也使用中文名称,建议改掉这种习惯。
5.  在使用输入输出流的时候错误使用了标示符“>>”“<<”,例cout>>a;
调试器错误信息:例:error C2676: binary '>>' : 'class std::basic_ostream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
对于流操作的方向搞错是一个普遍错误,问题本来并不复杂,可能是由于没有认真看书的原因。
6.  定义的变量类型与使用不对应,如声明为float,但实际给与了一个double的值,例:
float pi=3.412345245656245;
调试器错误信息:warning C4305: 'initializing' : truncation from 'const double' to 'float'
7.变量在赋值之前就使用,例:int a, b, c; c=a+b; cin>>a>>b;
    调试器错误信息:warning C4700: local variable 'a' used without having been initialized
       出现这种错误主要是对面向过程的程序执行没有理解。
8.  在一个工程中包含多于一个的main函数
error C2556: 'int __cdecl main(void)' : overloaded function differs only by return type from 'void __cdecl main(void)'
        E:\temp\alskdf\ldid.cpp(4) : see declaration of 'main'
E:\temp\alskdf\ldid.cpp(15) : error C2371: 'main' : redefinition; different basic types
在上机课中始终有同学犯这种错误,原因可能是从来没有上过机。
9.  在函数定义的()后面使用分号
例:void chang();
       {
}
调试器错误信息:error C2447: missing function header (old-style formal list?)
10.  函数声明/定义/调用参数个数不匹配.
例:void chang(int a,int b, float c)
{
 …
}
void main()
{
 …
 chang(3,4);
 }
调试器错误信息:error C2660: 'chang' : function does not take 2  parameters
 
 
 
最常见的20种VC++编译错误信息
1、   atal error C1010: unexpected end of file while looking for precompiled header directive。 
寻找预编译头文件路径时遇到了不该遇到的文件尾。(一般是没有#include "stdafx.h")
2、fatal error C1083: Cannot open include file: 'R…….h': No such file or directory
不能打开包含文件“R…….h”:没有这样的文件或目录。
3、error C2011: 'C……': 'class' type redefinition
类“C……”重定义。
4、error C2018: unknown character '0xa3'
不认识的字符'0xa3'。(一般是汉字或中文标点符号)
5、error C2057: expected constant expression
希望是常量表达式。(一般出现在switch语句的case分支中)
6、error C2065: 'IDD_MYDIALOG' : undeclared identifier
“IDD_MYDIALOG”:未声明过的标识符。
7、error C2082: redefinition of formal parameter 'bReset'
函数参数“bReset”在函数体中重定义。
8、error C2143: syntax error: missing ':' before '{'
句法错误:“{”前缺少“;”。
9、error C2146: syntax error : missing ';' before identifier 'dc'
句法错误:在“dc”前丢了“;”。
10、error C2196: case value '69' already used
值69已经用过。(一般出现在switch语句的case分支中)
11、error C2509: 'OnTimer' : member function not declared in 'CHelloView'
成员函数“OnTimer”没有在“CHelloView”中声明。
12、error C2511: 'reset': overloaded member function 'void (int)' not found in 'B'
重载的函数“void reset(int)”在类“B”中找不到。
13、error C2555: 'B::f1': overriding virtual function differs from 'A::f1' only by return type or calling convention
类B对类A中同名函数f1的重载仅根据返回值或调用约定上的区别。
14、error C2660: 'SetTimer' : function does not take 2 parameters
“SetTimer”函数不传递2个参数。
15、warning C4035: 'f……': no return value
“f……”的return语句没有返回值。
16、warning C4553: '= =' : operator has no effect; did you intend '='?
没有效果的运算符“= =”;是否改为“=”?
17、warning C4700: local variable 'bReset' used without having been initialized
局部变量“bReset”没有初始化就使用。
18、error C4716: 'CMyApp::InitInstance' : must return a value
“CMyApp::InitInstance”函数必须返回一个值。
19、LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing
连接错误:不能打开P1.exe文件,以改写内容。(一般是P1.Exe还在运行,未关闭)
20、error LNK2001: unresolved external symbol "public: virtual _ _thiscall C……::~C……(void)"
连接时发现没有实现的外部符号(变量、函数等)。


本文转自jazka 51CTO博客,原文链接:http://blog.51cto.com/jazka/162748,如需转载请自行联系原作者

相关文章
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
C++ 编译错误 error: ‘cout‘ was not declared in this scope (摄氏度与华氏度的转换)
|
5天前
|
安全 程序员 编译器
【实战经验】17个C++编程常见错误及其解决方案
想必不少程序员都有类似的经历:辛苦敲完项目代码,内心满是对作品品质的自信,然而当静态扫描工具登场时,却揭示出诸多隐藏的警告问题。为了让自己的编程之路更加顺畅,也为了持续精进技艺,我想借此机会汇总分享那些常被我们无意间忽视却又导致警告的编程小细节,以此作为对未来的自我警示和提升。
|
5月前
|
编译器 程序员 C++
【C/C++ 泛型编程 进阶篇】C++模板推导的迷宫:导致编译错误的原因及其解决策略
【C/C++ 泛型编程 进阶篇】C++模板推导的迷宫:导致编译错误的原因及其解决策略
105 2
|
IDE 编译器 开发工具
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
善用 vs 中的错误列表和输出窗口,高效查找 C++ 多工程编译错误
|
安全 编译器 C语言
C++——常见错误总结1.4
C++——常见错误总结1.4
C++(常见错误总结1.4)
C++(常见错误总结1.4)
|
安全 C++ Windows
C++(常见错误总结1.2,1.3)
C++(常见错误总结1.2,1.3)
|
存储 程序员 C++
C++(常见错误总结1)
C++(常见错误总结1)
BXA
|
C++
C++使用中需要避免的10个常见错误
C++使用中需要避免的10个常见错误
BXA
227 0
|
Java C++
C++编译错误解决办法:Class_Label[abi:cxx11]'被多次定义
C++编译错误解决办法:Class_Label[abi:cxx11]'被多次定义
158 0