OOP C++第一个编译器错误
OOP C++ first compiler error
请帮助这个新手,这是我的代码:
#include <iostream>
using namespace std;
class Complex {
private:
float r, i;
public:
Complex(float rr, float ii) : r(rr), i (ii) {}
float GiveRe () { return r; }
float GiveIm () { return i; }
void Setit (float rr, float ii) {
r = rr;
i = ii;
}
};
Complex a(10, 20);
Complex sumit (Complex &ref) {
static Complex sum (0, 0);
sum.Setit(sum.GiveRe() + ref.GiveRe(), sum.GiveIm() + ref.GiveIm());
return sum;
}
int main () {
Complex sumvalue = sumit (a);
cout << sumvalue << endl;
return 0;
}
错误:no match for 'operator<<' in 'std::cout << sumvalue'
。
程序应该输出一个复数的和。
cout不能告诉您想要输出什么,您需要指定运算符<lt;或者可以将类隐式转换为兼容类型。
http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/
Rudolf Mühlbauer在课堂上实现的代码:
将其添加到类标题中的某个位置:
friend ostream& operator<<(ostream& out, const Complex& compl);
标题下面的这个:
ostream& operator<<(ostream& out, const Complex& compl)
{
return out << compl.r << "/" << compl.i;
}
应更改实施方式以满足您的确切需求。
Complex没有运算符<lt;
ostream& Complex::operator << ( ostream& os )
{
// use os << field/method here to out put
return os;
}
此外,如果复杂可以以不同的方式显示到控制台,那么您应该考虑使用方法来显示,而不是cout<lt;
void Complex::DisplayToConsole()
{
std::cout << r << " " << i << 'n';
}
您必须重载Complex类型的"<<"
运算符。
#include <iostream>
using namespace std;
class Complex {
private:
float r, i;
public:
Complex(float rr, float ii) : r(rr), i (ii) {}
float GiveRe () { return r; }
float GiveIm () { return i; }
void Setit (float rr, float ii) {
r = rr;
i = ii;
}
};
ostream& operator<<(ostream& os, Complex& c)
{
float i;
os<<c.GiveRe();
if(c.GiveIm() < 0){
os<<"-j"<<c.GiveIm()*(-1);
}else{
os<<"+j"<<c.GiveIm();
}
return os;
}
Complex a(10, 20);
Complex sumit (Complex &ref) {
static Complex sum (0, 0);
sum.Setit(sum.GiveRe() + ref.GiveRe(), sum.GiveIm() + ref.GiveIm());
return sum;
}
int main () {
Complex sumvalue = sumit (a);
cout << sumvalue << endl;
return 0;
}
一个完整的最小示例:
#include <iostream>
using namespace std;
class C {
public:
int r, l;
// if the operator needs access to private fields:
friend ostream& operator<< (ostream&, const C&);
};
ostream& operator << (ostream& stream, const C& c) {
stream << c.r << "--" << c.l;
return stream;
}
int main() {
C c;
c.l = 1;
c.r = 2;
cout << c << endl;
}
C++允许您定义运算符。STL使用<<
运算符进行输出,整个istream/ostream类层次结构使用该运算符进行输入/输出。
运算符是作为函数实现的,但始终遵循非常特定的语法。与示例中一样,ostream& operator << (ostream&, const MYTYPEHERE&)
是定义ostream<<
运算符的方法。
当C++遇到一个语句时,它必须推导出所有操作数的类型,并找到(事实上非常神奇)这个问题的解决方案:给定我的操作数和运算符,我能找到一个使语句有效的类型吗?
这些流运算符是为<iostream>
中的所有基本类型定义的,所以若编写cout << 10
,编译器会找到一个运算符ostream& operator<< (ostream&, int)
。
如果你想在这个游戏中使用用户定义的类型,你必须定义运算符。否则,语句cout << sometype
将无效。这也是C++中有时会出现严重编译器错误的原因:"好吧,我定义了一些运算符<<,但没有一个与您的类型兼容!"。
因为有时为您的类型实现运算符是不有利的(例如,如果您只输出一次运算符),我建议编写:
cout << sum.re << "--" << sum.im << endl; // or similar
通过这种方式,您可以编写更少的代码,并且可以灵活地使用输出格式。谁知道你下次是否希望你的复数格式不同?但这是另一场讨论。
为什么这么复杂?因为C++可能非常复杂。它非常强大,但充满了特殊的语法和例外。最后,与C的区别就在这里:C++在类型推理(模板所需)方面做得更好,通常会产生WTF?
关于如何在代码中实现它,我认为其他答案提供了很好的解决方案!
sumit(a)
返回一个类型为Complex
的对象,而cout
没有定义为处理该对象。
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误