• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KingofNoobs

unique_ptr private member

14 posts in this topic

Hello,

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.
0

Share this post


Link to post
Share on other sites
Hello again,

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
0

Share this post


Link to post
Share on other sites
BitMaster,

Sorry about that. I have edited the post above. Also, the error is:

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
0

Share this post


Link to post
Share on other sites
Without knowing the details it is not possible to be sure but does a comparison like
(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.
0

Share this post


Link to post
Share on other sites
The original problem was that I was having to default construct animations. For my game, animations come out of a file called an "atlas" file. There should be no way to default construct either an atlas or a sprite, or an animation (the ordering is atlas->sprite->animation, each one owning one or more of the item following it on the list). Thus, when I want to create a SpriteInstance, I have to pass a "starting animation" but rather than default construct one and fill in the fields by hand, I want to force myself to use the "unique" and only copy instantiated by the file. So I thought that changing the atlas and sprite classes to hold pointers to animations rather than animations themselves, that I could solve this problem. Wanting to avoid memory leaks, I thought I would go with the unique_ptr, however, I'm not sure if it is possible given the current errors. I think it should be but don't want to waste too much time on the issue, when I could just use raw pointers and delete.

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>
0

Share this post


Link to post
Share on other sites
Hello,

I just realized that I am trying to share the pointers, so shared_ptr will be better. Thanks for your help.

-Dave Ottley
1

Share this post


Link to post
Share on other sites
I just want to point out that this

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
0

Share this post


Link to post
Share on other sites
There's no point in adding the std::move() call. The unique_ptr is already an rvalue because it's a temporary.
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1354405810' post='5006134']
There's no point in adding the std::move() call. The unique_ptr is already an rvalue because it's a temporary.
[/quote]

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?
0

Share this post


Link to post
Share on other sites
No, every temporary is automatically an rvalue; it's part of the definition of rvalue. It's possible to bind a temporary to a named non-rvalue reference, but that named reference is itself not a temporary.
0

Share this post


Link to post
Share on other sites
For the record, I think I found where I got myself confused from back where I read [url=http://thbecker.net/articles/rvalue_references/section_05.html]this[/url]. An rvalue reference can (easily) become an unexpected lvalue (because it has a name). In the following scenario the std::move is needed to use the move assignment operator:
[code]
void doSomething(X&& x)
{
X anotherX = std::move(x);
// whatever...
}
[/code]
0

Share this post


Link to post
Share on other sites
Of course in this case neither a temporary unique_ptr nor a rvalue reference to one needs to be used. std::vector::emplace_back() can be used to construct the object in place directly without bothering with a move.
[code]
mAtlasList.emplace_back(new Atlas(MASTER_ATLAS_MAINMENU));
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0