2013-10-26 65 views
-3

我得到与VS2013下面的代码的错误...的std ::矢量<性病::的unique_ptr <X>>成员变量引起问题

class Bob 
{ 
private: 
    std::vector<std::unique_ptr<UnrelatedClass>> _aVector; 
    inline Bob(const Bob & other) {}; 
    inline Bob & operator=(const Bob & other) { return *this; }; 
public: 
    Bob(); 
    ~Bob(); 
    Bob(Bob && o); 
}; 

class Fred 
{ 
    friend class Bob; 

    [... A few functions, Bob never used ...] 
}; 

...其中编译具有去除的细friend class Bob;。这是一个编译器错误?我不确定我做错了什么。

的错误如下:

c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(615): error C2248: 'std::unique_ptr<Unrelated,std::default_delete<_Ty>>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<Unrelated,std::default_delete<_Ty>>' 
     with 
     [ 
      _Ty=Unrelated 
     ] 
     c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1487) : see declaration of 'std::unique_ptr<Unrelated,std::default_delete<_Ty>>::unique_ptr' 
     with 
     [ 
      _Ty=Unrelated 
     ] 
     c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(614) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' 
     with 
     [ 
      _Ty=std::unique_ptr<Unrelated,std::default_delete<Unrelated>> 
     ] 
     c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(752) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled 
     with 
     [ 
      _Ty=std::unique_ptr<Unrelated,std::default_delete<Unrelated>> 
     ] 
     c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(580) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled 
     with 
     [ 
      _Ty=std::unique_ptr<Unrelated,std::default_delete<Unrelated>> 
     ] 
     c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled 
     with 
     [ 
      _Alloc=std::allocator<std::unique_ptr<Unrelated,std::default_delete<Unrelated>>> 
     ] 
     c:\...\include\bob.h(51) : see reference to class template instantiation 'std::vector<std::unique_ptr<Unrelated,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled 
     with 
     [ 
      _Ty=Unrelated 
     ] 

的研究使我明白这是当Bob复制产生同样的错误。因此,复制std::vector,因此复制std::unique_ptr

+1

我保证你不会“编译好”,因为你到处都丢失了分号。这段代码是谎言。 –

+0

2缺少分号。固定。你现在可以帮我吗? – AStupidNoob

+0

“我得到一个错误......” - ***你得到了什么错误*** –

回答

3

我找到了解决方案。看来问题是部分是的一个编译问题,主要是我的错。更新我的编译器会产生更好的错误信息,描述使用删除的函数作为问题的原因。

我追踪的问题到以下行:

_aVector.push_back(someUniquePtr); 

本应是:

_aVector.push_back(std::move(someUniquePtr)); 

我只好下次要多加小心;)

相关问题