经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++编译错误杂记
来源:cnblogs  作者:o0阿Q0o  时间:2018/12/11 9:29:37  对本文有异议

目录

2018年12月10日

g++ error: expected ‘)’ before ‘*’ token

  • 自定义头文件与系统中的文件重叠,导致自定义文件没有被加载
  • file.h是系统定义的头文件
  1. // 错误
  2. #define FILE_H
  3. #include "file.h"
  4. #endif
  5. // 正确
  6. #define FILE_H
  7. #include "myfile.h"
  8. #endif

参考资料
error: expected ')' before '*' token

2018年11月15日

error: invalid conversion from ‘const char*’ to ‘char*’

  • 常量指针无法直接赋值给普通指针,如果不得不赋值,要通过强制类型转换
  • 但是如果赋值过程发生在函数变量处,不会报错,可能是因为自动进行了强制类型转换
  1. // 错误
  2. char* prt;
  3. prt = str;
  4. // 正确
  5. char* prt;
  6. prt = (char*)str;

2018年11月11日

error: a storage class can only be specified for objects and functions

  • 声明自定义类时,不能加static
  1. // 错误
  2. // file: C.h
  3. class C{
  4. };
  5. // file: main
  6. #include "C.h"
  7. static class C;
  8. ... ...
  9. // 正确
  10. // file: C.h
  11. class C{
  12. };
  13. // file: main
  14. #include "C.h"
  15. class C;
  16. ... ...

error: cannot call member function ××× without object

  • 调用类内的函数时,必须通过实例来调用,不可以直接通过类名调用
  1. // 错误
  2. class C{
  3. int func(){}
  4. };
  5. int c = C::func();
  6. ... ...
  7. // 正确
  8. class C{
  9. int func(){}
  10. };
  11. C temp;
  12. int c = temp.func();
  13. ... ...

error: uninitialized reference member in ××× [-fpermissive]

  • 没有初始化引用
  1. // 错误
  2. class C{
  3. int a;
  4. int &r;
  5. C();
  6. };
  7. C::C(){
  8. a = 1;
  9. }
  10. ... ...
  11. // 正确
  12. class C{
  13. int a;
  14. int &r;
  15. C();
  16. };
  17. C::C():r(a){
  18. a = 1;
  19. }
  20. ... ...

error: passing ‘const ×××’ as ‘this’ argument discards qualifiers [-fpermissive]

  • 调用const变量时,相关函数没有明确是const的,可能存在被修改风险
  1. // 错误
  2. #include<iostream>
  3. class C{
  4. private:
  5. int a;
  6. public:
  7. bool func(){
  8. cout << "hello: " << this->a << endl;
  9. }
  10. C(const C& c){
  11. c.func();
  12. }
  13. };
  14. C c1;
  15. C c2(c1);
  16. // 正确
  17. #include<iostream>
  18. class C{
  19. private:
  20. int a;
  21. public:
  22. bool func() const{
  23. cout << "hello: " << this->a << endl;
  24. }
  25. C(const C& c){
  26. c.func();
  27. }
  28. };
  29. C c1;
  30. C c2(c1);

参考资料
error: passing 'const …' as 'this' argument of '…' discards qualifiers
error:passing 'const Student' as 'this' argument of 'void Student::print()' discards qualifiers

error: default argument given for parameter 2 of ×××

  • c++可以在类的声明中,也可以在函数定义中声明缺省参数,但不能既在类声明中又在函数定义中同时声明缺省参数。
  1. // 错误
  2. class C{
  3. C(int a=1);
  4. }
  5. C::C(int a=1){;}
  6. // 正确
  7. class C{
  8. C(int a=1);
  9. }
  10. C::C(int a){;}
  11. };

2018年11月10日

error: new types may not be defined in a return type

  • 定义类时在最后“}”后面没有“;”
  1. // 错误
  2. class C{
  3. ... ...
  4. }
  5. // 正确
  6. class C{
  7. ... ...
  8. };

error: two or more data types in declaration of ...

  • 头文件相互包含,使用如下方法解决
  1. #ifdef ×××× //定义一个宏,通常是该头文件名大写
  2. #define ××××
  3. #endif

error: ... does not name a type

  • 定义该类时,没有用class关键字,例如:
  1. // 定义C类
  2. // 错误
  3. C{
  4. ... ...
  5. };
  6. // 正确
  7. class C{
  8. ... ...
  9. };

error: stray ‘\357’ in program

  • 一般是出现了中文符号

error: ××× does not name a type

  • 在包含相应有文件的情况下,仍然会出现没有定义的情况,这是因为没有在该文件下声明该类型,如下:
  1. // 错误
  2. // file: C.h
  3. class C{
  4. ... ...
  5. };
  6. // file: test.h
  7. #include "C.h"
  8. C test;
  9. ... ...
  10. // 正确
  11. // file: C.h
  12. class C{
  13. ... ...
  14. };
  15. // file: test.h
  16. #include "C.h"
  17. class test;
  18. C test;
  19. ... ...

error: ‘virtual’ outside class declaration

  • 在类的外部定义虚函数时,不用再声明为"virtual"
  1. // 错误
  2. class C{
  3. virtual C();
  4. };
  5. virtual C(){
  6. ... ...
  7. }
  8. // 正确
  9. class C{
  10. virtual C();
  11. };
  12. C(){
  13. ... ...
  14. }

error: expected primary-expression before ‘const’

  • 因为在调用函数时,仍然带有变量类型,如下:
  1. // 错误
  2. const char* pathname = "hello.txt";
  3. oflag = O_RDWR|O_APPEND;
  4. this->_fd = open(const char* pathname, int oflag);
  5. // 正确
  6. const char* pathname = "hello.txt";
  7. oflag = O_RDWR|O_APPEND;
  8. int fd = open(pathname, oflag);
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号