OOP C++第一个编译器错误

OOP C++ first compiler error

本文关键字:错误 编译器 第一个 C++ OOP      更新时间:2023-10-16

请帮助这个新手,这是我的代码:

#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没有定义为处理该对象。