C++ 11 std::unique_ptr related compiling error
-
I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?
Quote:
e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my code:
#include
#include
#include
#includestruct Baa {
std::unique_ptr x;Baa() { } Baa(Baa&& o) { x = std::move(o.x); } Baa operator = (Baa&& param) { x = std::move(param.x); return \*this; }
};
int main()
{
std::map sheep;
Baa ba;
sheep[0] = ba;
} -
I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?
Quote:
e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my code:
#include
#include
#include
#includestruct Baa {
std::unique_ptr x;Baa() { } Baa(Baa&& o) { x = std::move(o.x); } Baa operator = (Baa&& param) { x = std::move(param.x); return \*this; }
};
int main()
{
std::map sheep;
Baa ba;
sheep[0] = ba;
} -
I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?
Quote:
e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my code:
#include
#include
#include
#includestruct Baa {
std::unique_ptr x;Baa() { } Baa(Baa&& o) { x = std::move(o.x); } Baa operator = (Baa&& param) { x = std::move(param.x); return \*this; }
};
int main()
{
std::map sheep;
Baa ba;
sheep[0] = ba;
} -
I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?
Quote:
e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my code:
#include
#include
#include
#includestruct Baa {
std::unique_ptr x;Baa() { } Baa(Baa&& o) { x = std::move(o.x); } Baa operator = (Baa&& param) { x = std::move(param.x); return \*this; }
};
int main()
{
std::map sheep;
Baa ba;
sheep[0] = ba;
}the problem is simple. The code
sheep[0] = ba;
should be
sheep[0] = std::move(ba);
Your first approach used the copy constructor to copy the unique_ptr this defeats the purpose unique_ptr. Also another issue is:
Baa operator = (Baa&& param)
should be
Baa& operator = (Baa&& param)