gpt4 book ai didi

c++ - 重载 operator<< operator== 和 operator!=

转载 作者:行者123 更新时间:2023-11-28 03:17:32 29 4
gpt4 key购买 nike

家庭作业

必须同时重载 operator<<、operator== 和 operator!=

.h文件和.cpp文件如下:

namespace JoePitz
{
class Complex
{
// declare friend functions
friend ostream &operator<<(ostream &out, const Complex &value);
friend ostream &operator<<(ostream &out, const bool &value);
friend istream &operator>>(istream &in, Complex &value);

public:
// constructor
Complex(double real, double imaginary);

// overloading +/-/==/!= operators
Complex operator+(const Complex &compx2);
Complex operator-(const Complex &compx2);
bool operator==(const Complex &compx2);
bool operator!=(const Complex &compx2);

private:
double real;
double imaginary;
void initialize(double real, double imaginary);
};

// GCC requires friend functions to be declared in name space
ostream &operator<<(ostream &out, const Complex &value);
ostream &operator<<(ostream &out, const bool &value);
istream &operator>>(istream &in, Complex &value);
}

excerpt from .cpp file

ostream& JoePitz::operator<<(ostream &out, const Complex &value)
{
// print real
cout << value.real;

// print imaginary
if (value.imaginary == ISZERO)
{
cout << POSSIGN << value.imaginary << IMAGNSGN;
}
else if (value.imaginary > ISZERO)
{
cout << POSSIGN << value.imaginary << IMAGNSGN;
}
else
{
cout << value.imaginary << IMAGNSGN;
}

return out;

}

ostream& JoePitz::operator<<(ostream &out, const bool &value)
{
return out;
}

// overloaded == operator
bool JoePitz::Complex::operator==(const Complex &compx2)
{
return (this->real == compx2.real && this->imaginary == compx2.imaginary);
}

// overloaded != operator
bool JoePitz::Complex::operator!=(const Complex &compx2)
{
return !(this->real == compx2.real && this->imaginary == compx2.imaginary);
}

我收到以下编译错误:

../src/hw4.cpp:71: 错误:'c1 << "* * *\012"' 中的 'operator<<' 不匹配../src/Complex.h:54: 注意:候选人是:std::ostream& JoePitz::operator<<(std::ostream&, const bool&)../src/Complex.h:53: 注意:std::ostream& JoePitz::operator<<(std::ostream&, const JoePitz::Complex&)

据我了解,这是因为不知道要实现哪个重载函数。

我遇到的问题是如何处理 operator<< 函数返回一个 ostream 并接受一个 Complex 对象,但 operator== 函数返回一个 bool。

但我不知道如何更改 operator== 函数来处理 bool 和/或 Complex 对象。我试图添加另一个返回 bool 的重载 opperator<< 函数,但编译器仍然有问题。

如有任何帮助,我们将不胜感激。

最佳答案

这是一个简单的运算符优先级错误,这个

cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n";

应该是这样

cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << (c1 == c1) << " * * *\n";

<<优先级高于 ==所以你需要括号。

移除 ostream bool 重载。

另一个变化

  Complex operator+(const Complex &compx2);
Complex operator-(const Complex &compx2);
bool operator==(const Complex &compx2);
bool operator!=(const Complex &compx2);

应该都是常量方法

  Complex operator+(const Complex &compx2) const;
Complex operator-(const Complex &compx2) const;
bool operator==(const Complex &compx2) const;
bool operator!=(const Complex &compx2) const;

关于c++ - 重载 operator<< operator== 和 operator!=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380944/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com