【问题标题】:no matching member function for call to 'erase'没有匹配的成员函数调用“擦除”
【发布时间】:2012-02-06 03:17:19
【问题描述】:

这是导致错误的代码:

Factory.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);
                         ~~~~~~~^~~~~

有什么建议吗? 谢谢。

【问题讨论】:

  • it2 需要什么? _table.erase(it++) 怎么样?

标签: c++ templates factory


【解决方案1】:

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

void erase( iterator position );

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

我该如何解决这个问题?

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

iterator erase( const_iterator position );

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

【讨论】:

  • 谢谢。我正在为此撕毁我的头发。
【解决方案2】:

您将 const_iterator 传递给需要普通迭代器的方法。

见:http://www.cplusplus.com/reference/stl/map/erase/

【讨论】:

    【解决方案3】:

    看看大师怎么说:

    Scot Meyers 的有效 STL

    Item 26. 使用迭代器优于 const iterator、reverse_iterator 和 const_reverse_iterator。尽管容器支持四种迭代器类型,但其中一种类型具有其他类型所没有的特权。那个类型是迭代器,迭代器是特殊的。

    typedef deque<int> IntDeque; //STL container and
    typedef lntDeque::iterator Iter; // iterator types are easier
    typedef lntDeque::const_iterator ConstIter; // to work with if you
    // use some typedefs
    Iter i;
    ConstIter ci;
    … //make i and ci point into
    // the same container
    if (i == ci ) ... //compare an iterator
    // and a const_iterator
    

    第 27 项。使用 distance 和 Advance 将容器的 const_iterators 转换为迭代器。

    typedef deque<int> IntDeque; //convenience typedefs
    typedef lntDeque::iterator Iter;
    typedef lntDeque::const_iterator ConstIter;
    ConstIter ci; // ci is a const_iterator
    …
    Iter i(ci); // error! no implicit conversion from
    // const_iterator to iterator
    Iter i(const_cast<Iter>(ci)); // still an error! can't cast a
    // const_iterator to an iterator
    

    有效的是前进和距离

    typedef deque<int> IntDeque; //as before
    typedef IntDeque::iterator Iter;
    typedef IntDeque::const_iterator ConstIter;
    IntDeque d;
    ConstIter ci;
    … // make ci point into d
    Iter i(d.begin()); // initialize i to d.begin()
    Advance(i, distance(i, ci)) //move i up to where ci is
    // (but see below for why this must
    // be tweaked before it will compile)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多