C ++ - 模板函数调用运算符重载 - 错误C2064

时间:2012-09-20 20:59:21

标签: c++ templates operator-overloading function-call-operator

我正在尝试在c ++中重载函数调用操作符,我得到了这个无法解决的编译错误(Visual Studio 2010)。

错误在行act(4);

#include <stdio.h>
#include <iostream>

void Test(int i);
template <class T> class Action
{
    private:
        void (*action)(T);
    public:
        Action(void (*action)(T))
        {
            this->action = action;
        }
        void Invoke(T arg)
        {
            this->action(arg);
        }
        void operator()(T arg)
        {
            this->action(arg);
        }
};

int main()
{
    Action<int> *act = new Action<int>(Test);
    act->Invoke(5);
    act(4);     //error C2064: term does not evaluate to a function taking 1 arguments overload
    char c;
    std::cin >> c;

    return 0;
}

void Test(int i)
{
    std::cout << i;
}

1 个答案:

答案 0 :(得分:8)

act仍然是你必须首先取消引用它的指针,如下所示:

(*act)(4);