加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在地图中使用std :: unique_ptr作为关键字

发布时间:2020-12-16 07:11:44 所属栏目:百科 来源:网络整理
导读:我正在使用Visual Studio 2012.我有一个如下所示的地图: std::mapstd::string,std::mapstd::unique_ptrsf::Sound,std::unique_ptrsf::SoundBuffer listSoundContainer; 我正在尝试插入这样的数据: std::unique_ptrsf::SoundBuffer soundBuffer(new sf::Sou
我正在使用Visual Studio 2012.我有一个如下所示的地图:

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

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

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)));               
}

并在编译时得到以下错误:

microsoft visual studio 11.0vcincludeutility(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.0vcincludememory(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.0vcincludexmemory0(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插入数据同样的问题.我错过了什么?我一直试图解决这个问题2个小时,现在无法理解它.

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

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;

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

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

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

你似乎想做的是制作一些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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读