使用地图内的std :: unique_ptr作为键 [英] Using std::unique_ptr inside a map as a key

查看:106
本文介绍了使用地图内的std :: unique_ptr作为键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio2012.我有一张看起来像这样的地图:

I am using Visual Studio 2012. I have a map that looks like this:

std::map<std::string,std::map<std::unique_ptr<sf::Sound>,std::unique_ptr<sf::SoundBuffer>>> listSoundContainer;

我正在尝试插入这样的数据:

I'm trying to insert data like this:

std::unique_ptr<sf::SoundBuffer> soundBuffer(new sf::SoundBuffer());
if (soundBuffer->loadFromFile("assets/sound/" + _fileName) != false)
{
    std::unique_ptr<sf::Sound> sound(new sf::Sound(*soundBuffer));
    typedef std::map<std::unique_ptr<sf::Sound>, std::unique_ptr<sf::SoundBuffer>> innerMap;
    listSoundContainer[_fileName].insert(innerMap::value_type(std::move(sound), std::move(soundBuffer)));               
}

并在编译时出现以下错误:

and im getting the following error at compile time:

Microsoft Visual Studio 11.0 \ vc \ include \ utility(182):错误C2248: 'std :: unique_ptr< _Ty> :: unique_ptr':无法访问私有成员 在类'std :: unique_ptr< _Ty>'1>中用1>
声明 [1> _Ty = sf ::声音1>] 1> c:\ program 档案(x86)\ Microsoft Visual Studio 11.0 \ vc \ include \ memory(1447):请参阅 'std :: unique_ptr< _Ty> :: unique_ptr'的声明1>带有1> [1> _Ty = sf ::声音1>] 1> c:\ program 文件(x86)\ Microsoft Visual Studio 11.0 \ vc \ include \ xmemory0(617): 请参见对功能模板实例化的参考 'std :: pair< _Ty1,_Ty2> :: pair(std :: pair< _Ty1,_Ty2> &&,void **)'被1>编译为1> [1>
_Ty1 = const std :: unique_ptr,1> _Ty2 = std :: unique_ptr,1> _Kty = std :: unique_ptr,1> _Ty = std :: unique_ptr 1>]

microsoft visual studio 11.0\vc\include\utility(182): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1>
[ 1> _Ty=sf::Sound 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=sf::Sound 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory0(617) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair(std::pair<_Ty1,_Ty2> &&,void **)' being compiled 1> with 1> [ 1>
_Ty1=const std::unique_ptr, 1> _Ty2=std::unique_ptr, 1> _Kty=std::unique_ptr, 1> _Ty=std::unique_ptr 1> ]

我也曾尝试使用make_pair插入数据,但存在相同的问题.我想念什么?我已经尝试解决这个问题两个小时了,无法解决这个问题.

I have also tried to insert data using make_pair with the same problem. What am I missing? Ive been trying to solve this problem for 2 hours now and can't get my head around it.

我实际上可以通过不使用智能指针来解决此问题:

I can actually solve this problem by NOT using smart pointers:

sf::SoundBuffer* soundbuffer = new sf::SoundBuffer();
soundbuffer->loadFromFile(_file);
sf::Sound* sound = new sf::Sound(*soundbuffer);
typedef std::map<sf::SoundBuffer*, sf::Sound*> mapType;
listSound[_file].insert(mapType::value_type(soundbuffer, sound));

推荐答案

查看std::map的模板定义:

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

现在让我们看看如何实例化它:

And now lets look at how you try to instantiate it:

std::map<
    std::string, 
    std::map<
        std::unique_ptr<sf::Sound>, 
        std::unique_ptr<sf::SoundBuffer>
    >
> 
listSoundContainer

这里的问题是std::unique_ptr<sf::Sound>不能充当键.

The problem here is that a std::unique_ptr<sf::Sound> cannot act as a key.

您似乎想做的是列出std::pair<std::unique_ptr<sf::Sound>, std::unique_ptr<sf::SoundBuffer>>

What you seem trying to do is to make some kind of list of std::pair<std::unique_ptr<sf::Sound>, std::unique_ptr<sf::SoundBuffer>>

我建议改用它:

std::map<
    std::string, 
    std::list<
        std::pair<
            std::unique_ptr<sf::Sound>, 
            std::unique_ptr<sf::SoundBuffer>
        >
    >
> 
listSoundContainer

这篇关于使用地图内的std :: unique_ptr作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆