重载运算符<< operator ==和operator!=

时间:2013-05-05 02:54:26

标签: c++ overloading

家庭作业

必须重载运算符<<,lt =,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:错误:不匹配&#39;运营商&lt;&lt;&lt;&#39; in&#39; c1&lt;&lt; &#34; * * * \ 012&#34;&#39; ../src/Complex.h:54:注意:候选人是:std :: ostream&amp; JoePitz :: operator&lt;&lt;(std :: ostream&amp;,const bool&amp;) ../src/Complex.h:53:注意:std :: ostream&amp; JoePitz :: operator&lt;&lt;(std :: ostream&amp;,const JoePitz :: Complex&amp;)

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

我遇到的问题是如何处理运营商&lt;&lt; function返回一个ostream并接受一个Complex对象,但是operator == function返回一个bool。

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

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

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);

应该都是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;