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. ...
随机推荐
- 无缓冲和带缓冲channel的区别
常规定义的channel都是默认不带缓冲的,如下代码所示 package main import ( "fmt" ) func main() { c := make(chan in ...
- 设计模式:命令(Command)模式
设计模式:命令(Command)模式 一.前言 命令也是类,将命令作为一个类来保存,当要使用的时候可以直接拿来使用,比如脚本语言写出的脚本,只需要一个命令就能执行得到我们想要的需要操作很长时间才能得到 ...
- 微软在线 VSTS/TFS 使用简介,如何删除项目,帐号,获取git地址等
名称:微软 VSTS 全称: Visual Studio Team Services 地址:https://www.visualstudio.com/zh-hans/ 说明:注册就可以了使用了(如何使 ...
- 理解Underscore中的_.template函数
Underscore中提供了_.template函数实现模板引擎功能,它可以将JSON数据源中的数据对应的填充到提供的字符串中去,类似于服务端渲染的模板引擎.接下来看一下Underscore是如何实现 ...
- hdu-1695 GCD---容斥定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目大意: 求解区间[1, n]和[1, m]中有多少对不同的x和y使得gcd(x, y) = ...
- mongodb在windows平台安装和启动
mongodb 官网:https://www.mongodb.com mongodb 官网下载: mongodb-win32-x86_64-2008plus-ssl-3.4.2-signed.msi ...
- [转] 从此不再惧怕URI编码:JavaScript及C# URI编码详解
混乱的URI编码 JavaScript中编码有三种方法:escape.encodeURI.encodeURIComponent C#中编码主要方法:HttpUtility.UrlEncode.Serv ...
- 2019.1.4 SSH框架整合步骤(一)
SSH整合 1.三大框架整合原理 Spring与Struts2整合就是将Action对象交给Spring容器负责创建 Spring与Hibernate整合就是将sessionFactory交给Spri ...
- rnnlm学习
rnn-lm: 1.论文 2.公式推导 2.1 http://blog.csdn.net/a635661820/article/details/44462315 3. 工具 lstm-lm 1. 论文 ...
- 记录 iOS 各种跳转到系统应用
MARK ----拨打电话 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; if (phoneVersion.fl ...