Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Cannot overload functions distinguished by return type alone" mean?

Tags:

c++

I have this code:

In the header:

...
int32_t round(float v);
...

and in the source

...
int32_t round(float v)
{
    int32_t t = (int32_t)std::floor(v);
    if((v - t) > 0.5)
        return t + 1;

    return t;
}
...

I've looked around here on this site but the examples seem a bit too complicated to me.

I'm learning C++ so if someone could explain to me what the error means and why it's occurring I would be grateful.

like image 289
dominique120 Avatar asked Feb 09 '15 22:02

dominique120


People also ask

Why we Cannot overload a function on return type?

The return type of a function has no effect on function overloading, therefore the same function signature with different return type will not be overloaded. Example: if there are two functions: int sum() and float sum(), these two will generate a compile-time error as function overloading is not possible here.

Can you overload a function based on return type?

No, you cannot overload a method based on different return type but same argument type and number in java.


1 Answers

Function overloading means to have multiple methods with the same name.

Now, the compiler, to resolve the correct overloaded method, looks at method name and arguments but NO at the return value. This means that if you have

int round(float something) { ... }
float round(float something) { ... }

Then the compiler is not able to distinguish them and know which one you want to invoke at a call point. So in your case this means that there is already another round method which accepts a float.

like image 73
Jack Avatar answered Nov 02 '22 10:11

Jack