"Cannot overload functions distinguished by return type alone"是什么意思?
What does "Cannot overload functions distinguished by return type alone" mean?
我有这个代码:
在标题中:
...
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;
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- "Cannot overload functions distinguished by return type alone"是什么意思?