我有这个代码:
在标题中:
...
int32_t round(float v);
...
并且在源中
...
int32_t round(float v)
{
int32_t t = (int32_t)std::floor(v);
if((v - t) > 0.5)
return t + 1;
return t;
}
...
我在这个网站上看了看,但这些例子对我来说有点太复杂了
我正在学习C++,所以如果有人能向我解释这个错误的含义以及为什么会发生,我将不胜感激。
函数重载意味着拥有多个同名方法。
现在,为了解析正确的重载方法,编译器会查看方法名称和参数,但不会查看返回值。这意味着如果你有
int round(float something) { ... }
float round(float something) { ... }
然后编译器无法区分它们,也无法知道要在调用点调用哪一个。因此,在您的情况下,这意味着已经有另一个接受float
的round
方法。
C++标准不支持仅基于返回类型的重载,这是因为返回类型在确定被调用的函数方面没有发挥作用。返回类型仅在运行时确定,因此它不是编译器用来决定调用哪个函数的函数签名的一部分。
#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;
}