[英]std::pair of std::string and custom class fails to copy on std::map::insert(std::make_pair(string, class))
This error: 这个错误:
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility 216
occurs on this function's only line: 发生在这个函数的唯一一行:
void Animation::AddAnimation(std::string name, AnimationFrameSet& animation) {
_animations.insert(std::make_pair(name, animation));
}
_animations
is a std::map<std::string, AnimationFrameSet>
_animations
是一个std::map<std::string, AnimationFrameSet>
AnimationFrameSet declares an operator=(...) and a copy constructor, but weirdly enough, the compiler says it fails on the attempted copying of const std::string
...even though the string isn't even being passed in as a const
. AnimationFrameSet声明了一个operator =(...)和一个复制构造函数,但奇怪的是,编译器说它在尝试复制const std::string
时失败了......即使const std::string
甚至没有被传入const
。
I can't for the life of me figure out (or even remember! :P) why this is/should throwing/throw an error. 我不能为我的生活弄清楚(甚至记住!:P)为什么这是/应该抛出/抛出错误。
Thanks. 谢谢。
EDIT 编辑
The reason I'm a little confused why this is not working is that a different class uses a very similar implementation and it does not throw an error: 我有点困惑为什么这不起作用的原因是一个不同的类使用非常相似的实现,它不会抛出错误:
BITMAP* BitmapCache::GetBitmap(std::string filename) {
//Return NULL if a bad filename was passed.
if(filename.empty()) return NULL;
if(exists(filename.c_str()) == false) return NULL;
//Reduce incorrect results by forcing slash equality.
filename = fix_filename_slashes(&filename[0]);
//Clean the cache if it's dirty.
CleanCache();
//Search for requested BITMAP.
MapStrBmpIter _iter = _cache.find(filename);
//If found, return it.
if(_iter != _cache.end()) return _iter->second;
//Otherwise, create it, store it, then return it.
BITMAP* result = load_bmp(filename.c_str(), NULL);
if(result == NULL) return NULL;
/*Similar insert line, a non-const std::string that was passed in is passed to a std::make_pair(...) function*/
_cache.insert(std::make_pair(filename, result));
return result;
}
typedefs: 类型定义:
typedef std::map<std::string, BITMAP*> MapStrBmp;
typedef MapStrBmp::iterator MapStrBmpIter;
Since _animations
is a std::map
, try using another method of inserting, such as: 由于_animations
是一个std::map
,尝试使用另一种插入方法,例如:
_animations[name] = animation;
But the more important thing to check would be if the AnimationFrameSet class does have a valid copy constructor and assignment operator. 但更重要的是检查AnimationFrameSet类是否具有有效的复制构造函数和赋值运算符。 If it doesn't you may want to use a smart pointer class such as: 如果不是,您可能希望使用智能指针类,例如:
typedef std::shared_ptr<AnimationFrameSet> AnimationFrameSetPtr;
Then the map would be: 然后地图将是:
std::map<std::string, AnimationFrameSetPtr>
The reason why this is failing is because the std::map container you are using expects a const value as key value. 这是失败的原因是因为您使用的std :: map容器需要一个const值作为键值。
Check the documentation of std::map here . 在这里查看std :: map的文档。
Example: 例:
class TestClass
{
};
std::map< const std::string, TestClass > myMap;
std::pair< const std::string, TestClass > firstElement = std::make_pair("test", TestClass() );
myMap.insert( firstElement );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.