1

我正在尝试在 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;
}
4

1 回答 1

8

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

(*act)(4);
于 2012-09-20T21:01:30.797 回答