c++ - 指向成员函数的模板化函数指针

标签 c++ templates function-pointers

大家好,新年快乐等

我在模板化一些函数指针时遇到了问题:

目前的代码如下:

    template<class T>
    class EventMapper
    {
    private:
        typedef std::wstring const (T::*messageHandler)(std::wstring const & myMessage);                //!< Templated function pointer
        typedef std::tr1::unordered_map<std::wstring, messageHandler> umap;                             //!< abbreviation for eventHandler map container
        typedef umap::const_iterator eventCIt;                                                          //!< abbreviation for event map const_iterator
        typedef umap::iterator  eventIt;                                                                //!< abbreviation for event map iterator

        //test func ptr
        T const & myInstance;
        umap myEventMap;
        eventCIt myCurrentCommand;                                                                      //!< current selected command
    public:
        EventMapper( T const & instance_in) : myInstance(instance_in){}

        //register an event handler
        void registerHandler(std::wstring const & cmd_in, messageHandler handler_in)
        {
            this->myEventMap.insert(umap::value_type(cmd_in, handler_in));
        }

我在 MSVS 2008 SP1 下遇到这个错误:

Error   3   error C2146: syntax error : missing ';' before identifier 'eventCIt'    o:\AX_FusRecAlg\include\Reconstruction\JobListEditor\Types.h    19  AX.Services.Reconstruction.JobListDataProviderTest

描述性不强。我正在尝试做的事情有可能吗?欢迎任何提示!谢谢。

最佳答案

要修复您的代码,请使用 typename :

        typedef typename umap::const_iterator eventCIt;                                                          //!< abbreviation for event map const_iterator
        typedef typename umap::iterator  eventIt;

更多信息,看template dependent names

关于c++ - 指向成员函数的模板化函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8817217/

相关文章:

c++ - 将缺少初始值设定项的数组作为参数传递时编译失败

c++ - getaddrinfo() 函数抛出错误 n 11003

c++ - 非原子类型如何实现std::atomic_ref?

c++ - 构建 Arduino 库

c++ - 模板函数中的 "C4430: missing type specifier - int assumed"

c++ - 检索对象的函数运算符的参数类型

c++ - 模板的非静态成员可以专用于数据或函数吗?

c++ - 我无法访问指向成员的指针。为什么?

c - 在函数中按地址交换两个参数时出错

c++ - find_if 错误 : invalid initialisation of reference of type 'const node&' from expression of type 'node*'