【问题标题】:Function Pointer to member function of template class? (C++)指向模板类成员函数的函数指针? (C++)
【发布时间】:2011-12-09 14:00:36
【问题描述】:

在我一直在处理的一项任务中,我一直在努力解决这个问题,但似乎根本无法让它发挥作用。我写了一个小测试类来演示我想要做什么,希望有人能解释我需要做什么。

//Tester class
#include <iostream>
using namespace std;

template <typename T>
class Tester
{
    typedef void (Tester<T>::*FcnPtr)(T);

private:
    T data;
    void displayThrice(T);
    void doFcn( FcnPtr fcn );

public:
    Tester( T item = 3 );
    void function();
};

template <typename T>
inline Tester<T>::Tester( T item )
    : data(item)
{}

template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
    //fcn should be a pointer to displayThrice, which is then called with the class data
    fcn( this->data );
}

template <typename T>
inline void Tester<T>::function() 
{
    //call doFcn with a function pointer to displayThrice()
    this->doFcn( &Tester<T>::displayThrice );
}

template <typename T>
inline void Tester<T>::displayThrice(T item)
{
    cout << item << endl;
    cout << item << endl;
    cout << item << endl;
}

-这是主要内容:

#include <iostream>
#include "Tester.h"
using namespace std;

int main()
{
    Tester<int> test;
    test.function();

    cin.get();
    return 0;
}

-最后,我的编译器错误(VS2010)

    c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(28): error C2064: term does not evaluate to a function taking 1 arguments
1>          c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(26) : while compiling class template member function 'void Tester<T>::doFcn(void (__thiscall Tester<T>::* )(T))'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(21) : while compiling class template member function 'Tester<T>::Tester(T)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\name\documents\visual studio 2010\projects\example\example\example.cpp(7) : see reference to class template instantiation 'Tester<T>' being compiled
1>          with
1>          [
1>              T=int
1>          ]

希望我在 Tester 课程中的 cmets 会告诉你我想要做什么。感谢您抽出宝贵时间查看此内容!

【问题讨论】:

  • 如果合适,请确保添加作业标签。另外,看看boost::bind,特别是boost::mem_fn

标签: c++ function pointers member


【解决方案1】:

你没有正确调用成员函数指针;它需要使用称为pointer-to-member operator 的特殊运算符。

template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
    (this->*fcn)( this->data );
    //   ^^^
}

【讨论】:

  • 大家好,在 OS X 和 NetBeans 中尝试这段代码给我带来 2 个奇怪的错误 架构 x86_64 的未定义符号:“Tester::function()”,引用自:main 中的 _main .o“Tester::Tester(int)”,引用自:main.o 中的 _main ld:未找到架构 x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 到见调用),有什么想法吗?
【解决方案2】:

您需要显式添加您发送消息的对象:

(*this.*fcn)(this->data); // << '*this' in this case

另见C++ FAQ

【讨论】:

    【解决方案3】:

    要通过指向成员函数的指针和实例指针调用成员函数,您需要-&gt;* 语法,注意运算符优先级:

    (this->*fcn)(data);
    

    【讨论】:

    • 有效!太感谢了。试图弄清楚这一切是一场充满丑陋语法的噩梦。
    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多