C++编译错误杂记

2018年12月23日

error: no matching function for call to ×××

  • 表明没有找到匹配函数的相关参数,可能的缘由有:
    1. 参数类型不匹配;
    2. 参数构造错误,例如构造函数CLRead read_buff()就是一种错误的构造,致使read_buff没法被识别, 出现note: no known conversion for argument 1 from ‘CLRead (*)()’ to ‘CLRead*’
    3. 函数名称写错。

2018年12月10日

error: expected ‘)’ before ‘*’ token

  • 文件中的一些类没有被声明。致使的缘由多是:
    1. 自定义头文件与系统中的文件重叠,致使自定义文件没有被加载。可是 file.h是系统定义的头文件。这致使本身定义的file.h被覆盖。
    2. 加载了正确的自定义头文件,可是没有声明要使用的自定义类。
// 错误
#define FILE_H
#include "file.h"
#endif

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

class CLFileBuff;  // 声明后才能使用,要否则也会出现这类错误

参考资料
error: expected ')' before '*' tokenc++

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 qualifiersexpress

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);