【发布时间】:2011-12-31 16:55:21
【问题描述】:
我遇到了一些非常奇怪的错误。由于某种我不明白的原因,编译器似乎想要调用复制构造函数。
(118) std::map<int, layer> xs;
(119) xs.begin()->first; // error?!
layer 是不可复制的可移动字体。
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,_Ty2>' being compiled
1> with
1> [
1> _Ty1=const int,
1> _Ty2=layer
1> ]
我也尝试了以下方法,但同样失败。
(118) std::map<int, layer> xs;
(119) auto& t1 = *xs.begin();
(120) auto& t2 = t1.first; // error?!
这是怎么回事?
【问题讨论】:
-
我为什么需要那个?我只是在读取成员变量“first”的值。
-
好吧,也许是私有复制构造函数的问题,检查一下:stackoverflow.com/questions/1440287/… 第一个错误清楚地表明尝试访问 layer::layer 失败,因为它是私有的。
-
您的代码示例使用 Visual Studio 2010 SP1 对我来说编译得很好(没有可观察的基类)。
-
对空集合调用
begin会产生 UB。你能发布一个真实的例子吗? SSCCE -
这并不能改变这不是一个真实例子的事实。如果您真的需要帮助,请发布一些独立的内容,而不仅仅是两三行代码。
标签: c++ visual-studio-2010 compiler-errors c++11