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.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插入数据,但是出现了相同的问题。我错过了什么?我已经试图解决这个问题两个小时了,但是还是无法理解。
实际上,我可以通过不使用智能指针来解决这个问题:
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));
map::value_type
。文档说它将键值转换为常量。 - meneldalunique_ptr
作为map
的键类型非常奇怪,甚至是不可能的,因为Meneldal所述的原因。即使您设法让代码编译通过,您如何找到具有匹配键的内容呢?毕竟,unique_ptr
是唯一的。即使您设法以某种方式构造另一个管理相同原始指针的unique_ptr
,一旦您完成了find
或使用operator[]
进行索引,第二个unique_ptr
将被销毁,从而删除它所管理的指针。您需要想出其他数据结构。 - Praetorian