C++编译错误杂记

时间:2018-12-10
本文章向大家介绍C++编译错误杂记,主要包括C++编译错误杂记相关应用实例、知识点总结和注意事项,具有一定的参考价值,需要的朋友可以参考一下。

目录

2018年12月10日

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

  • 自定义头文件与系统中的文件重叠,导致自定义文件没有被加载
  • file.h是系统定义的头文件
// 错误
#define FILE_H
#include "file.h"
#endif

// 正确
#define FILE_H
#include "myfile.h"
#endif

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

2018年11月15日

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

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

// 正确
char* prt;
prt = (char*)str;

2018年11月11日

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

  • 声明自定义类时,不能加static
// 错误
// file: C.h
class C{
};
// file: main
#include "C.h"
static class C;
... ...

// 正确
// file: C.h
class C{
};
// file: main
#include "C.h"
class C;
... ...

error: cannot call member function ××× without object

  • 调用类内的函数时,必须通过实例来调用,不可以直接通过类名调用
// 错误
class C{
    int func(){}
};

int c = C::func();
... ...

// 正确
class C{
    int func(){}
};

C temp;
int c = temp.func();
... ...

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

  • 没有初始化引用
// 错误
class C{
    int a;
    int &r;
    C();
};
C::C(){
    a = 1;
}
... ...

// 正确
class C{
    int a;
    int &r;
    C();
};
C::C():r(a){
    a = 1;
}
... ...

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

  • 调用const变量时,相关函数没有明确是const的,可能存在被修改风险
// 错误
#include<iostream>
class C{
private:
    int a;
public:
    bool func(){
        cout << "hello: " << this->a << endl;
        }
    C(const C& c){
        c.func();
    }
};
C c1;
C c2(c1);

// 正确
#include<iostream>
class C{
private:
    int a;
public:
    bool func() const{
        cout << "hello: " << this->a << endl;
        }
    C(const C& c){
        c.func();
    }
};
C c1;
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++可以在类的声明中,也可以在函数定义中声明缺省参数,但不能既在类声明中又在函数定义中同时声明缺省参数。
// 错误
class C{
   C(int a=1);
}
C::C(int a=1){;}

// 正确
class C{
   C(int a=1);
}
C::C(int a){;}
};

2018年11月10日

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

  • 定义类时在最后“}”后面没有“;”
// 错误
class C{
    ... ...
}

// 正确
class C{
    ... ...
};

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

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

error: ... does not name a type

  • 定义该类时,没有用class关键字,例如:
// 定义C类
// 错误
C{
    ... ...
};

// 正确
class C{
    ... ...
};

error: stray ‘\357’ in program

  • 一般是出现了中文符号

error: ××× does not name a type

  • 在包含相应有文件的情况下,仍然会出现没有定义的情况,这是因为没有在该文件下声明该类型,如下:
// 错误
// file: C.h
class C{
    ... ...
};
// file: test.h
#include "C.h"
C test;
... ...

// 正确
// file: C.h
class C{
    ... ...
};
// file: test.h
#include "C.h"
class test;

C test;
... ...

error: ‘virtual’ outside class declaration

  • 在类的外部定义虚函数时,不用再声明为"virtual"
// 错误
class C{
    virtual C();
};

virtual C(){
    ... ...
}

// 正确
class C{
    virtual C();
};

C(){
    ... ...
}

error: expected primary-expression before ‘const’

  • 因为在调用函数时,仍然带有变量类型,如下:
// 错误
const char* pathname = "hello.txt";
oflag = O_RDWR|O_APPEND;
this->_fd = open(const char* pathname, int oflag);

// 正确
const char* pathname = "hello.txt";
oflag = O_RDWR|O_APPEND;
int fd = open(pathname, oflag);