【问题标题】:C++ function pointer with templates带有模板的 C++ 函数指针
【发布时间】:2015-11-06 19:55:34
【问题描述】:

我正在尝试存储指向成员函数的指针。需要存储指针的类声明为:

template <typename TDataType, typename T>
bool my_function(std::string topic_name,
                 void(T::*)(const TDataType&) fp,
                 T* obj)

我得到错误:

error: expected ',' or '...' before 'fp'
                               void(T::*)(const TDataType&) fp,

谁能告诉我发生了什么事?看起来这是我没有得到的语法错误。

【问题讨论】:

    标签: c++ pointers member-function-pointers


    【解决方案1】:

    变化:

    void(T::*)(const TDataType&) fp
    

    void(T::* fp)(const TDataType&)
    

    【讨论】:

      【解决方案2】:

      成员函数指针的语法是

      return_type(class_name::* function_pointer_name)(function_parameters)
      

      所以

      template <typename TDataType, typename T>
      bool my_function(std::string topic_name,
                       void(T::*)(const TDataType&) fp,
                       T* obj)
      

      需要

      template <typename TDataType, typename T>
      bool my_function(std::string topic_name,
                       void(T::* fp)(const TDataType&)'
                       T* obj)
      

      【讨论】:

        猜你喜欢
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 2015-01-20
        • 2017-10-20
        • 2016-10-18
        • 1970-01-01
        相关资源
        最近更新 更多