gpt4 book ai didi

c++ - 没有匹配的成员函数调用 'erase'

转载 作者:可可西里 更新时间:2023-11-01 17:37:22 26 4
gpt4 key购买 nike

这是导致错误的代码:

工厂.h:

#include <string>
#include <map>

namespace BaseSubsystems
{
template <class T>
class CFactory
{
protected:
typedef T (*FunctionPointer)();
typedef std::pair<std::string,FunctionPointer> TStringFunctionPointerPair;
typedef std::map<std::string,FunctionPointer> TFunctionPointerMap;
TFunctionPointerMap _table;
public:
CFactory () {}
virtual ~CFactory();
}; // class CFactory

template <class T>
inline CFactory<T>::~CFactory()
{
TFunctionPointerMap::const_iterator it = _table.begin();
TFunctionPointerMap::const_iterator it2;

while( it != _table.end() )
{
it2 = it;
it++;
_table.erase(it2);
}

} // ~CFactory
}

我得到的错误:

error: no matching member function for call to 'erase' [3]
_table.erase(it2);
~~~~~~~^~~~~

有什么建议吗?谢谢。

最佳答案

这是map::erase的签名在 C++98 中:

void erase( iterator position );

此函数接受一个iterator,但您传递的是一个const_iterator。这就是代码无法编译的原因。

How do I fix this?

在 C++11 中,这甚至不是问题,因此不需要修复。这是因为在 C++11 中,map::erase 函数具有以下签名,因此接受 const_iterator

iterator erase( const_iterator position );

如果您不能使用新标准,则必须将变量更改为 iterator

关于c++ - 没有匹配的成员函数调用 'erase',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com