#1 Members - Reputation: 301
Posted 28 November 2012 - 04:14 AM
Can someone tell me why line (1) does not generate a compiler error while line (2) does?
mAtlasList.push_back(std::unique_ptr<Atlas>(new Atlas(MASTER_ATLAS_MAINMENU)) );
(1)mAtlasList.begin()->operator*().GetName();
(2)auto IsLevel1Atlas = [](std::unique_ptr<Atlas> atlas) -> bool { return atlas->GetName() == L"Level1Atlas"; };
auto tempAtlasPtr = std::find_if(mAtlasList.begin(), mAtlasList.end(), IsLevel1Atlas);
const Sprite * tempSpritePtr = &(tempAtlasPtr->operator*().GetSpriteList().find(L"Background")->second);
The error generated is: error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
Thanks.
I wonder as I wander...
#2 Members - Reputation: 683
Posted 28 November 2012 - 04:21 AM
Change IsLevel1Atlas to
auto IsLevel1Atlas = [](const std::unique_ptr<Atlas> & atlas) -> bool { return atlas->GetName() == L"Level1Atlas"; };
The first one works I guess your list::push_back must implement rvalue reference (&&) version?
Edited by wqking, 28 November 2012 - 04:24 AM.
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
#4 Members - Reputation: 301
Posted 28 November 2012 - 05:22 AM
Is something like this possible? I am getting the same error here, which leads me to believe that a vector<unique_ptr<somethin> > is not possible:
const Rect GetSourceRect(const std::unique_ptr<Animation>& currentAnimation, uint32_t frameNumInAnimation) const
{
// Calculate position of current animation frame
auto i = std::find(mAnimations.begin(), mAnimations.end(), currentAnimation); // error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
As you can see I passed the unique_ptr as a const &, but when I use algorithms, there appear to be copies being made here and there. Is there a solution to this?
Thanks,
-Dave Ottley
Edited by KingofNoobs, 28 November 2012 - 05:31 AM.
I wonder as I wander...
#7 Members - Reputation: 1403
Posted 28 November 2012 - 05:39 AM
(instance of std::unique_ptr<somethin>) == (instance of std::unique_ptr<Animation>)
make sense at all? You might have to define a custom predicate for whatever it is you want and use std::find_if.
On a sidenote, the error is still not complete the output will contain a few more lines after the actual error codes to define exactly what kind of types T is in each case.
#8 Members - Reputation: 301
Posted 28 November 2012 - 05:47 AM
The full error is below:
error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=Animation
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=Animation
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1> with
1> [
1> _Ty=std::unique_ptr<Animation>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Animation>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include ype_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Animation>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<std::unique_ptr<Animation>>
1> ]
1> c:\users\mrmerchantman\dropbox\zzz_c++\projects\blockbuster\source\graphics\sprite.h(37) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<Animation>
I wonder as I wander...
#10 Members - Reputation: 751
Posted 01 December 2012 - 05:42 PM
mAtlasList.push_back(std::unique_ptr<Atlas>(new Atlas(MASTER_ATLAS_MAINMENU)) );
should be
mAtlasList.push_back( std::move( std::unique_ptr<Atlas>(new Atlas(MASTER_ATLAS_MAINMENU)) ) );
But if you really are trying to pass around multiple smart pointers to the same object you should use shared_ptr
If this post was helpful and/or constructive please give rep.
SFML2.0 Nightly Download Link http://en.sfml-dev.org/forums/index.php?topic=9513.0
SFML2.0 Tutorials http://www.sfml-dev.org/tutorials/2.0/
#12 Members - Reputation: 1403
Posted 03 December 2012 - 04:38 AM
There's no point in adding the std::move() call. The unique_ptr is already an rvalue because it's a temporary.
I'm myself still a bit unsure about all the details of rvalues, but wouldn't it be more correct to say "unnamed temporary" because not every temporary automatically is an rvalue?
#14 Members - Reputation: 1403
Posted 03 December 2012 - 09:13 AM
void doSomething(X&& x)
{
X anotherX = std::move(x);
// whatever...
}
#15 Moderators - Reputation: 6645
Posted 03 December 2012 - 04:22 PM
mAtlasList.emplace_back(new Atlas(MASTER_ATLAS_MAINMENU));






