Question

I'm trying to overload function call operator in c++ and I got this compilation error that I cannot resolve (Visual Studio 2010).

Error is in line 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;
}
Was it helpful?

Solution

act is stil a pointer you have to dereference it first, like so:

(*act)(4);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top