我正在尝试实现一个 std::unordered_map,其中 std::string 作为键,std::unique_ptr 作为值。但是,当我尝试编译时,出现错误:
错误 C2338:C++ 标准不提供此类型的散列。
环顾四周,我知道 C++11 确实包含一个 std::hash < std::string >,我看不出为什么会抛出这个错误。我已经尝试实现我自己的散列函数,就像看到的那个 here , 但它仍然抛出相同的错误。我也尝试过使用 __declspec(dllexport)
并将包含类的复制构造函数和赋值运算符设为私有(private),正如某些线程中建议的那样使 unique_ptr 工作,但无济于事。
这是违规类的代码:
#ifndef __TEXTURE_MAP_H__
#define __TEXTURE_MAP_H__
#include <unordered_map>
#include <vector>
#include <memory>
#include <string>
//__declspec for std::unique_ptr compat.
class /*__declspec(dllexport)*/ TextureMap : virtual public IconRegister
{
private:
uint32 _textureId;
std::unordered_map<const std::string, std::unique_ptr<AtlasTexture> > _registeredIcons;
std::unordered_map<const char*, AtlasTexture*> _uploadedIcons;
std::vector<AtlasTexture*> _animatedIcons;
public:
TextureMap();
~TextureMap();
uint32 getTextureId();
void loadTextureAtlas();
/* override */ IIcon& registerIcon(const char*);
void registerIcons();
private:
TextureMap(const TextureMap& other) { }
TextureMap& operator= (const TextureMap& other) { return *this; };
};
#endif
我找不到任何原因这不应该工作,并且我已经尝试了几乎所有我在搜索问题时可以找到的其他解决方案。
我正在使用 MSVC 2012。
非常感谢任何帮助。谢谢。
编辑:添加 AtlasTexture 类:header和 implementation
编辑:我对移动和移动分配的实现:here .
最佳答案
您是否尝试过在 AtlasTexture 中实现所有编译生成的方法?
关于c++ - C2338 : The C++ Standard doesn't provide a hash for this type with string and unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21747156/