目录

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 '*' 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++编译错误杂记的更多相关文章

  1. xamarin.forms新建项目android编译错误

    vs2015 update3 新建的xamarin.forms项目中的android项目编译错误.提示缺少android_m2repository_r22.zip,96659D653BDE0FAEDB ...

  2. 《转载》使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误

    今天在更新项目后进行编译时,出现如下错误一堆: 编译错误 Google之,在stackoverflow上看到如下的解决方法: I came here with the same problem. Ev ...

  3. asp.net教程:编译错误同时存在于不同dll中

    asp.net 编译错误类型“同时存在于”不同的dll中. 出现这种错误大概有三种情况: 1.ASPX页面,一个*.ASPX,对应着一个*.cs文件,两者其实是一个文件,通过两者实现代码分离,每个*. ...

  4. VS2010出现FileTracker : error FTK1011编译错误的解决办法

    VS2010出现FileTracker : error FTK1011不知道是不是vs2010的一个bug,反正有人提交了. FileTracker : error FTK1011编译错误的解决办法有 ...

  5. PowerDesginer 生成的Oracle 11g 组合触发器代码编译错误(29): PLS-00103

    问题描述: 采用PowerDesigner15针对Oracle 11g 创建物理数据模型,想实现一个字段的自增,采用如下步骤: 1.创建序列,命名为Sequence_1; 2.在自增字段编辑窗口中,选 ...

  6. 我看见的第一个XCODE编译错误 - Command /applications.../clang failed with exit code 1

    开始用XCODE学习Apple相关开发的东东,写些demo熟悉Object C,一直还没看见什么问题,昨晚在家把一些demo上传到代码服务器,今天在另外一台机器上下载下来编译,出现了问题: Preco ...

  7. eclipse 编译android程序 编译错误

    windows->show view -> problems, 这个窗口的内容即为 编译错误的内容.

  8. 一个C++宏定义与枚举定义重复的编译错误

    C++的开发效率低是众所周知的,原因比如有: 语言复杂度高 编译效率低 工具链不够完整高效(尤其是linux下) 另外一个恐怕是不少编译错误让人摸不着头脑,今天碰到一个,举个例子: #include ...

  9. java编译错误 程序包javax.servlet不存在javax.servlet.*

    java编译错误 程序包javax.servlet不存在javax.servlet.* 编译:javac Servlet.java 出现 软件包 javax.servlet 不存在 软件包javax. ...

随机推荐

  1. 【Leetcode】【Easy】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  2. 设计模式:状态(State)模式

    设计模式:状态(State)模式 一.前言     状态模式在某些场合中使用是非常方便的,什么叫做状态,如果大家学过<编译原理>就会明白DFA M和NFA M,在确定有限状态机和非确定有限 ...

  3. 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创

    马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与   分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴 ...

  4. Cocos2d-x 3.1.1 学习日志3--C++ 初始化类的常量数据成员、静态数据成员、常量静态数据成员

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011292087/article/details/37598919 有关const成员.stati ...

  5. 【[NOI2016]区间】

    发现自己的离散化姿势一直有问题 今天终于掌握了正确的姿势 虽然这并不能阻挡我noip退役爆零的历史进程 还是先来看看离散化怎么写吧,我以前都是这么写的 for(std::set<int>: ...

  6. PHP-------MySQLi 的函数

    MySQLi 的函数 在数据库中找到一张是自增长的科目表表就可以, Code主键值是自增长的,name是varchar类型的. 如果想往科目表里添加一条数据,是自增长列的表中添加数据,添加完之后,取添 ...

  7. pcel安装的mongodb的两个问题的解决方案

    最近工作需要,要使用mongodb,这个是使用 pecl 安装的,跟标准的 mongo 使用还是有区别的,这里不讲区别,只讲两个比较典型的问题该如何处理,具体的文档大家可以直接参考 php 的官方文档 ...

  8. 前端使用ajax传到后台的实体类的多个属性,直接用Map接收

    前端ajax传过来的数据按照以上方法接收Map中 var ip = $("#ip").val(); var port = $("#port").val(); v ...

  9. 给windows添加路由

    route add 10.0.0.0 mask 255.0.0.0 172.16.1.253 -p

  10. Java中int和String的转换问题

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...