微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 没有匹配成员函数来调用’erase’

这是导致错误代码

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

有小费吗?
谢谢.

解决方法

这是C 98中 map::erase的签名:
void erase( iterator position );

这个函数需要一个迭代器,但是你传递了一个const_iterator.这就是代码无法编译的原因.

How do I fix this?

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

iterator erase( const_iterator position );

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

原文地址:https://www.jb51.cc/c/118739.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐