Question

I'm attempting to overload the () operator to accept a long, that code is below.

struct print{
    void operator()(long x){
        printf("Number: %d\n",x);
    }
};

However when I write this:

print p();
long l = 10;
p(l);

The compiler yells at me saying "error : too many arguments in function call" on the line that corresponds to p(l);

Why isn't this working, did I overload the () operator correctly?

Was it helpful?

Solution

You hit the most vexing parse. Change print p(); to print p;.

print p() is a declaration of a function named p which returns a print object.

Also, in C++, an empty parameter list means a function taking no arguments (same as (void)), which is why the compiler complains: " too many arguments in function call".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top