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.
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.
No, you cannot overload a method based on different return type but same argument type and number in java.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With