STL使用时的一个常见错误(拷贝构造函数)

本文介绍了在使用STL map的insert函数时遇到的错误,由于类的拷贝构造函数被定义为私有,导致无法插入对象。错误信息没有明确提示,定位困难。解决方法是避免直接拷贝类对象,改为使用指针或其他间接方式。
摘要由CSDN通过智能技术生成

背景

最近在协助同事定位问题的时候,发现一个有趣的问题。使用std::map的insert函数插入的时候,总是报了一个莫名的错误。std::map<std::string, T> map_data;而 T的类型,则是一个第三方的类类型。而看到的报错,则是完全没有头绪。

定位

说实话,如果再来一次,这种问题我来看错误,我是肯定想不到是这种原因导致的,因为错误信息中完全找不到跟问题相关的提示,这也是C++编绎器一直被诟病的一个原因。
这里把报错的信息贴出来,方便遇到类似问题的同学找到这个问题。

g++ -c test.cpp
test.cpp: In function ‘int main(int, char**)’:
test.cpp:37:46: error: no matching function for call to ‘std::map<std::__cxx11::basic_string, Cond>::insert(std::pair<std::__cxx11::basic_string, Cond>)’
map_test.insert(std::make_pair(str, cond));
^
In file included from /usr/local/gcc-7.1/include/c++/7.1.0/map:61:0,
from test.cpp:2:
/usr/local/gcc-7.1/include/c++/7.1.0/bits/stl_map.h:795:7: note: candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::__cxx11::basic_string; _Tp = Cond; _Compare = std::less<std::__cxx11::basic_string >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string, Cond> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值