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