[英]overloading operator<< operator== and operator!=
Homework assignment 家庭作业
Have to overload both the operator<<, operator== and operator!= 必须重载operator <<,operator ==和operator!=
.h file and .cpp file is included below: .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);
}
I received the following compile error: 我收到以下编译错误:
../src/hw4.cpp:71: error: no match for 'operator<<' in 'c1 << " * * *\\012"' ../src/Complex.h:54: note: candidates are: std::ostream& JoePitz::operator<<(std::ostream&, const bool&) ../src/Complex.h:53: note: std::ostream& JoePitz::operator<<(std::ostream&, const JoePitz::Complex&) ../src/hw4.cpp:71:错误:'c1 <<“中没有匹配'operator <<'* * * * 012”'../src/Complex.h:54:注意:候选人是: std :: ostream&JoePitz :: operator <<(std :: ostream&,const bool&)../ src /Complex.h:53:注意:std :: ostream&JoePitz :: operator <<(std :: ostream&,const JoePitz ::复杂&)
From my understanding this is a result from not knowing which overloaded function to implement. 根据我的理解,这是因为不知道要实现哪个重载函数。
The problem I am having is how to deal with the fact that the operator<< function return an ostream and accepts a Complex object, but the operator== function return a bool. 我遇到的问题是如何处理operator <<函数返回一个ostream并接受一个Complex对象这一事实,但是operator == function返回一个bool。
But I do not know how to change the operator== function to handle the bool and or the Complex object. 但我不知道如何更改operator ==函数来处理bool和/或Complex对象。 I have attempted to add another overloaded opperator<< function that return a bool but the compiler still has problems with it.
我试图添加另一个重载的opperator <<函数返回一个bool但编译器仍然有问题。
Any assistance will be greatly appreciated. 任何帮助将不胜感激。
It's a simple operator precedence error, this 这是一个简单的运算符优先级错误,这个
cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n";
should be this 应该是这个
cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << (c1 == c1) << " * * *\n";
<<
has higher precedence than ==
so you need the brackets. <<
具有比==
更高的优先级,因此您需要括号。
Remove the ostream bool overload. 删除ostream bool重载。
Another change 另一个变化
Complex operator+(const Complex &compx2);
Complex operator-(const Complex &compx2);
bool operator==(const Complex &compx2);
bool operator!=(const Complex &compx2);
should all be const methods 应该都是const方法
Complex operator+(const Complex &compx2) const;
Complex operator-(const Complex &compx2) const;
bool operator==(const Complex &compx2) const;
bool operator!=(const Complex &compx2) const;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.