整理并解释一些c++编译和运行错误

一、error: passing ‘const 类名' as ‘this’ argument of ‘bool 类名::函数名 (const XXX&)’

        字面翻译就可以大体知道原因,const XXX无法传递给this,类成员函数有一个隐藏参数,就是this,原因很明显了,const到非const转换。那怎么把this const了呢?

        声明const成员函数,也就是函数后面加上const

        例:

bool operator<(const Node& p2)

        改为

bool operator<(const Node& p2) const

二、xxx cannot have cv-qualifier

        cv是指const和volatile,也就是说前面的xxx多了cv修饰,找出去除,一般见于以下情况

        1)非类成员函数修饰 const

        2)静态成员不修饰 const

三、a lambda closure type has a deleted default constructor

         字面翻译:lamda类型拥有一个禁止调用的默认构造函数,也就是lamda默认构造对象。

        举一个场景:map使用lamda函数,由于map第三个参数接收的是类型,需要用decltype获得lamda类型

        auto fun = [](const Node&v1, const Node&v2){ return v1.val < v2.val };
        map<Node, int, decltype(fun)> obj;   //正确用法obj(fun)

        这个时候,在执行具体比较时,会用第三参数创建可执行对接,导致触发无参构造。

         解决办法就是传递比较对象进去,使用带一个参数的map构造方法

四、使用stl标准thread编译未加-lpthread导致的运行错误

Program received signal SIGSEGV, Segmentation fault

#0  0x00007ffff7dea45c in _dl_fixup () from /lib64/ld-linux-x86-64.so.2
#1  0x00007ffff7df0c55 in _dl_runtime_resolve () from /lib64/ld-linux-x86-64.so.2
#2  0x00007ffff7b8c5a7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib64/libstdc++.so.6
#3  0x0000000000404ae3 in std::thread::thread<void (ThreadPool::*)(int), ThreadPool* const, int&> (this=0x7fffffffe210, __f=@0x7fffffffe220)
    at /opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/thread:135

五、引用乱用,一些指针类型不能使用引用,比如函数指针,在typedef之后容易发生

no known conversion for argument 1 from ‘std::_Bind<void (*(int))(int)>’ to ‘TaskType& {aka std::function<void()>&}’

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值