C++编译错误杂记
目录
- 2018年12月23日
- 2018年12月10日
- 2018年11月15日
- 2018年11月11日
- error: a storage class can only be specified for objects and functions
- error: cannot call member function ××× without object
- error: uninitialized reference member in ××× [-fpermissive]
- error: passing ‘const ×××’ as ‘this’ argument discards qualifiers [-fpermissive]
- error: default argument given for parameter 2 of ×××
- 2018年11月10日
2018年12月23日
error: no matching function for call to ×××
- 代表没有找到匹配函数的相关参数,可能的原因有:
- 参数类型不匹配;
- 参数构造错误,例如构造函数
CLRead read_buff()就是一种错误的构造,导致read_buff无法被识别, 出现note: no known conversion for argument 1 from ‘CLRead (*)()’ to ‘CLRead*’; - 函数名称写错。
2018年12月10日
error: expected ‘)’ before ‘*’ token
- 文件中的一些类没有被声明。导致的原因可能是:
- 自定义头文件与系统中的文件重叠,导致自定义文件没有被加载。但是 file.h是系统定义的头文件。这导致自己定义的file.h被覆盖。
- 加载了正确的自定义头文件,但是没有声明要使用的自定义类。
// 错误
#define FILE_H
#include "file.h"
#endif
// 正确
#define FILE_H
#include "myfile.h"
#endif
class CLFileBuff; // 声明后才能使用,要不然也会出现这类错误
参考资料
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);
C++编译错误杂记的更多相关文章
- xamarin.forms新建项目android编译错误
vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...
- 《转载》使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误
今天在更新项目后进行编译时,出现如下错误一堆: 编译错误 Google之,在stackoverflow上看到如下的解决方法: I came here with the same problem. Ev ...
- asp.net教程:编译错误同时存在于不同dll中
asp.net 编译错误类型“同时存在于”不同的dll中. 出现这种错误大概有三种情况: 1.ASPX页面,一个*.ASPX,对应着一个*.cs文件,两者其实是一个文件,通过两者实现代码分离,每个*. ...
- VS2010出现FileTracker : error FTK1011编译错误的解决办法
VS2010出现FileTracker : error FTK1011不知道是不是vs2010的一个bug,反正有人提交了. FileTracker : error FTK1011编译错误的解决办法有 ...
- PowerDesginer 生成的Oracle 11g 组合触发器代码编译错误(29): PLS-00103
问题描述: 采用PowerDesigner15针对Oracle 11g 创建物理数据模型,想实现一个字段的自增,采用如下步骤: 1.创建序列,命名为Sequence_1; 2.在自增字段编辑窗口中,选 ...
- 我看见的第一个XCODE编译错误 - Command /applications.../clang failed with exit code 1
开始用XCODE学习Apple相关开发的东东,写些demo熟悉Object C,一直还没看见什么问题,昨晚在家把一些demo上传到代码服务器,今天在另外一台机器上下载下来编译,出现了问题: Preco ...
- eclipse 编译android程序 编译错误
windows->show view -> problems, 这个窗口的内容即为 编译错误的内容.
- 一个C++宏定义与枚举定义重复的编译错误
C++的开发效率低是众所周知的,原因比如有: 语言复杂度高 编译效率低 工具链不够完整高效(尤其是linux下) 另外一个恐怕是不少编译错误让人摸不着头脑,今天碰到一个,举个例子: #include ...
- java编译错误 程序包javax.servlet不存在javax.servlet.*
java编译错误 程序包javax.servlet不存在javax.servlet.* 编译:javac Servlet.java 出现 软件包 javax.servlet 不存在 软件包javax. ...
随机推荐
- Hibernate、Mybatis与Spring Data JPA
从零开始集成Springboot+MyBatis+JPA https://www.jianshu.com/p/e14c4a6f6871 MyBatis 与Hibernate的区别 http://xhr ...
- navicat远程连接mysql,2003 can't connect to mysql server on 10038
转载地址:https://blog.csdn.net/f12105212/article/details/70768516 1:我们连接远程服务器的mysql,如果出现问题,很大问题会出在服务器的端口 ...
- postgresql+postgis+pgrouting实现最短路径查询(3)--流程图
项目结束,做一个项目的总结汇报,就把最短路径查询的实现流程图画了一下,现在补出来:
- 关于notify() 和notifyAll() 一个需要注意的地方
notify() 和 notifyAll()都是唤醒其他正在等待同一个对象锁的线程. 下面是我遇到的一个问题,记下来,免得忘了. 直接上代码,有错误的代码: 代码描述:有一个Caculate类,类中又 ...
- springmvc(2)处理器设配器和映射器
非注解的处理器 映射器 和 适配器 一.处理器映射器 1.BeanNameUrlHandlerMapping <bean class="org.springframework.web ...
- centos 在安装YouCompleteMe时提示 Fatal : pyconfig.h No such file or directory
问题:centos 在安装YouCompleteMe时提示 Fatal : pyconfig.h No such file or directory 解决:安装python-devel yum ins ...
- 同源策略(same-origin policy)及三种跨域方法
同源策略(same-origin policy)及三种跨域方法 1.同源策略 含义: 同源是指文档的来源相同,主要包括三个方面 协议 主机 载入文档的URL端口 所以同源策略就是指脚本只能读取和所属文 ...
- 配置RedisTemplate、JedisPoolConfig、JedisConnectionFactory+自定义序列化 (xml+java方式)+使用
java方式配置RedisTemplate //spring注入ben //@Bean(name = "redisTemplate") public RedisTemplate i ...
- P1018 乘积最大(高精度加/乘)
P1018 乘积最大 一道dp题目.比较好像的dp题目. 然而他需要高精度计算. 所以,他从我开始学oi,到现在.一直是60分的状态. 今天正打算复习模板.也就有借口解决了这道题目. #include ...
- 【转】优秀的Java程序员必须了解GC的工作原理
一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,因为有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率 ,才能提高整个应 ...