- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新手程序员,我正在编写一个将两个复数相加的简单程序。我重载了 <<
通过以下方式:
ostream& operator << (ostream& output, Complex &complex_num){
output << complex_num.realPart << " + " << "(" << complex_num.imaginaryPart << ")i" <<endl;
return output;
}
我的加法函数如下:
Complex operator +(Complex &c2){
Complex temp;
temp.realPart=realPart+c2.realPart;
temp.imaginaryPart=imaginaryPart + c2.imaginaryPart;
return temp;
}
在我的主要功能中,当我尝试通过键入打印出结果时:
cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num1 + num2<< endl;
我收到一条错误消息,指出运算符 <<
不匹配.但是,当我分配另一个对象时 num3 = num1 + num2
然后编写如下代码,程序运行正常。
cout << "ADDITION OF THE TWO COMPLEX NUMBERS: "<<num3<< endl;
这里发生了什么?谁能帮帮我?
最佳答案
你的 operator+
返回 Complex
,这是一个临时对象。与 operator<<
一起使用时不起作用,因为您试图将它绑定(bind)到非常量引用参数。
一个变量可以绑定(bind)到一个非常量引用,这样就可以了。
修复方法是将参数设为 operator<<
通过常量引用:
ostream& operator << (ostream& output, Complex const &complex_num);
所以它现在也适用于临时工。更好的是,它也适用于 const 参数。
关于c++ - 获取错误 : no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'Complex' ) despite overloading the << operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62078649/
我的 friend 给我发了一个代码,他说在 Windows 中成功编译。我在linux上尝试过,但未能给出以下错误。下面是代码的最小可验证示例。 #include #include using
搜索这个问题的标题给了我很多人引用相同的错误,但在不同的情况下,不幸的是那里提供的答案是针对他们的情况的,我看不出他们能如何帮助我。 我正在尝试重载 operator template class
我已经看过几个关于这个的问题,特别是 Overloading operator&&’很有帮助。它让我知道我的问题是我正在做一些 c++11 无法从中推断出类型的事情。 我认为我的问题的很大一部分是我正
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
在这段代码中,我尝试将迭代器移动 10 个元素。 #include #include #include int main() { using
你好everonye即使我在这里找到了很多关于这个问题的答案,但一个人根本无法向我解释事情的进展如何无法解决它。 所以我的问题是我有一个名为 Matrix 的类,当我尝试将运算符 }’ lvalue
如何在 C++ 中打印 high_resolution_clock? #include #include typedef std::chrono::high_resolution_clock hi
关于这个标题的 StackOverflow 有很多关于这个标题的问题,但没有一个对我有帮助,我真的不明白发生了什么。 我正在尝试创建一个生成随机单词的类。首先,我尝试将所有元音和辅音放在两个不同的 s
我在尝试做一些简单的事情时遇到了一个令人困惑的错误消息 std::cout {1,2,3}; 上面写着 cannot bind 'std::ostream {aka std::basic_ostre
我在尝试编译我的代码时遇到此错误: 1> c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/ostream: In constructor 'Log
我想知道如何在 std::basic_ostream 中插入参数我一直在努力,但我做不到 我需要插入一个参数来选择我想要打印的 arista 值一旦我插入了参数,下一步就很容易了,因为它只是一个 if
根据 cppref , 有一个 operator 过载接受 const char* .似乎转换操作只是加宽了每个 char进入 wchar_t .也就是说,转换(插入)的宽字符数等于窄字符数。那么问
为什么使用用户定义的类进行流操作的典型 header C通常是这样的: std::ostream& operator>(std::istream& is, C&); 不是这样的: template
我想提供一个流运算符来输出 std::chrono::time_point 作为 GMT 日期,我目前有以下内容(仅针对 ostream 进行了简化): using datetime_t = std:
我正在阅读 basic_ostream 对象的 write 方法,这是我在 cppreference 上找到的内容: basic_ostream& write( const char_type* s,
这个问题在这里已经有了答案: Misunderstanding about ostream class and operator << (5 个答案) 关闭 8 年前。 标题是我的问题。 basic
我收到以下错误,我不确定是什么问题 1 IntelliSense: "std::basic_ostream::basic_ostream(const std::basic_ostream::_Myt
上面有转换吗? 最佳答案 将std::string 转换为输出 流?通常是:将字符串转换为从给定字符串中读取字符的输入流: std::string myString = //... std::istr
就在我认为我了解 C++11 中 iostream 的多遍移动构造时(感谢 https://stackoverflow.com/a/8156356/273767 的介绍),我遇到了这个: §27.7.
§27.7.3.9为 operator basic_ostream& operator&& os, const T& x); Effects: os > 的右值重载。) 基本上,它只是转发到左
我是一名优秀的程序员,十分优秀!