大佬教程收集整理的这篇文章主要介绍了c – 奇怪的错误 – 为什么编译器试图调用复制构造函数?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
(118) std::map<int,layer> xs; (119) xs.begin()->first; // error?!
图层是不可复制的可移动类型.
class layer : public observable { layer(const layer&); layer& operator=(const layer&); public: layer(int index = -1); layer(layer&& other); layer& operator=(layer&& other); //... };
由于某种原因,第119行导致编译器尝试为std :: pair调用复制构造函数,为什么?
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(131): error C2248: 'layer::layer' : cAnnot access private member declared in class 'layer' 1> ..\layer.h(55) : see declaration of 'layer::layer' 1> ..\layer.h(53) : see declaration of 'layer' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(129) : while compiling class template member function 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base(const std::_Pair_base<_Ty1,_Ty2> &)' 1> with 1> [ 1> _Ty1=const int,1> _Ty2=layer 1> ] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(174) : see reference to class template instantiation 'std::_Pair_base<_Ty1,_Ty2>' being compiled 1> with 1> [ 1> _Ty1=const int,1> _Ty2=layer 1> ] 1> ..\stage.cpp(119) : see reference to class template instantiation 'std::pair<_Ty1,1> _Ty2=layer 1> ]
我也试过以下,它也失败了.
(118) std::map<int,layer> xs; (119) auto& t1 = *xs.begin(); (120) auto& t2 = t1.first; // error?!
这里发生了什么?
在这种情况下,xs.first()导致生成std :: map< int,layer> :: iterator,这也需要生成std :: pair< int,layer>. std :: pair的默认实现有一个复制构造函数,无法编译.
你可以使用std :: pair的模板特化来解决这个问题,它没有复制构造函数,但是你不能在地图中插入任何东西. xs [0] = myLayer创建并插入std :: make_pair< 0,myLayer>进入你的地图,这显然需要一个图层的复制结构.
典型的解决方案是将您的类型更改为std :: map< int,std :: shared_ptr< layer>取代.复制shared_ptr不会复制引用的对象.
以上是大佬教程为你收集整理的c – 奇怪的错误 – 为什么编译器试图调用复制构造函数?全部内容,希望文章能够帮你解决c – 奇怪的错误 – 为什么编译器试图调用复制构造函数?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。