首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重载operator<< operator==和operator!=

重载operator<< operator==和operator!=
EN

Stack Overflow用户
提问于 2013-05-05 10:54:26
回答 1查看 936关注 0票数 0

家庭作业

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

.h文件和.cpp文件包含在下面:

代码语言:javascript
复制
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"‘../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并接受一个复杂对象,而operator==函数返回一个bool。

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

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-05 13:19:40

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

代码语言:javascript
复制
cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << c1 == c1 << " * * *\n";

应该是这样的

代码语言:javascript
复制
cout << "* * * Unit Test 3 comparison operations == != * * \n";
cout << " * * Print c1 == c1 = " << (c1 == c1) << " * * *\n";

<<==具有更高的优先级,因此您需要使用括号。

删除ostream bool过载。

另一个变化

代码语言:javascript
复制
  Complex operator+(const Complex &compx2);
  Complex operator-(const Complex &compx2);
  bool operator==(const Complex &compx2);
  bool operator!=(const Complex &compx2);

都应该是const方法

代码语言:javascript
复制
  Complex operator+(const Complex &compx2) const;
  Complex operator-(const Complex &compx2) const;
  bool operator==(const Complex &compx2) const;
  bool operator!=(const Complex &compx2) const;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16380944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档