0
我遇到問題。我有一個類重載運算符這樣的..使用const重載運算符<但不要插入到映射爲const
class Foo
{
friend bool operator<(const Foo &a, const Foo &b);
...
};
bool operator<(const Foo &a, const Foo &b)
{
return a.member < b.member;
}
然後在擁有一些FOOS在地圖中鍵一類的功能...
void Bar::Update()
{
for (FooItr itr = foos.begin(); itr != foos.end();) {
FooItr test = itr++;
if (!test->first.Check()) { // Check() is const
my_map.insert(*test);
foos.remove(test);
}
}
for (MapItr itr = my_map.begin(); itr != my_map.end(); ++itr) {
itr->first.Update(); // Update is not const
}
}
,我得到一個錯誤消息像...
error: passing ‘const Foo’ as ‘this’ argument of ‘void Foo::Update()’ discards qualifiers
我相信原因是my_map.insert()被插入常量FOOS,但我不知道如何解決這個問題。
+1,我錯過了 - >第一個:vP – Potatoswatter 2010-10-17 02:18:08
啊當然。 Nargh!謝謝。 – random 2010-10-17 02:18:59
那麼問題的完整背景是不存在的,但使用multimap解決它(通過完全避免問題,再次不會進入它)。儘管可能的解決方案。 – random 2010-10-17 02:29:31