'What does "Cannot overload functions distinguished by return type alone" mean?
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.
Solution 1:[1]
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
.
Solution 2:[2]
#include <iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
void set(int r, int i)
{
real = r;
img = i;
}
complex(int r = 0, int i = 0)
{
real = r;
img = i;
}
int getReal(){
return real ;
}
int getImg(){
return img ;
}
friend complex operator+(complex x, complex y);
friend void Display(complex y);
};
/*freind functions of complex*/
complex operator+(complex x, complex y)
{
complex z;
z.real = x.getReal() + y.getReal();
z.img = x.getImg() + y.getImg();
return z;
}
void Display(complex y)
{
cout << y.getReal()<< "+i" << y.getImg() << endl;
return 0;
}
int main()
{
complex c1, c2, c3;
c1.set(78, 9);
c2.set(68, 9);
c3 = c1 + c2;
c1.Display(c3);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jack |
Solution 2 |