当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++编译错误杂记

C++编译错误杂记

2018年12月12日  | 移动技术网IT编程  | 我要评论

美女随心贴,色戒被删除部分,悬赏 粤语

目录

2018年12月10日

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

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

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

参考资料

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网