c++ - 类中的运算符重载会导致范围错误

标签 c++ class operator-overloading complex-numbers

我正在尝试创建一个类以重载 + , - , * , / , << , >> ,和==为了处理复数。在尝试排除故障时,我收到多条错误消息:

error: ‘double complexType::imaginaryPart’ is private

我的头文件是

#ifndef COMPLEXTYPE_H
#define COMPLEXTYPE_H
#include <iostream>
#include <cmath>
#include <fstream>

class complexType
  {
    friend ostream& operator << (ostream &os, const complexType &a)
    friend istream& operator >> (istream &is, const complexType &a)
   public:
     complexType();
     complexType(double real, double imag );
     void getComplexType(double&, double&);
     void setComplextType(const double&, const double&);
     friend bool operator == (const complexType &otherComplex) const;
     friend complexType operator + (const complexType &, const complexType &);
     friend complexType operator - (const complexType &, const complexType &);
     friend complexType operator * (const complexType &, const complexType &);
     friend complexType operator / (const complexType &, const complexType &);
    private:
     double realPart; //Variable to store the real part
     double imaginaryPart; //Variable to store the private part
};
#endif // COMPLEXTYPE_H 

我的实现文件是

   #include "complexType.h"

   complexType::complexType()
   {
     realPart = 0.0;
     imaginaryPart = 0.0;
   }

   complexType::complexType(double r, double i)
   {
      realPart = r;
      imaginaryPart = i;
   }

   void setComplextType(double r, double i)
   {
     realPart = r;
     imaginaryPart = i;
   } 

   bool operator == (const complexType &a, const complexType &b)
   {
     return (a.realPart == b.realPart && a.imaginaryPart == b.imaginaryPart);
   }

     operator + (const complexType &a, const complexType &b) 
   {
     complexType temp;
     temp.realPart = a.realPart + b.realPart;
     temp.imaginaryPart = a.imaginaryPart + b.imaginaryPart;
     return temp;
    }

     operator - (const complexType &a, const complexType &b)
     {
        complexType temp;
        temp.realPart = a.realPart - b.realPart;
        temp.imaginaryPart = a.imaginaryPart - b.imaginaryPart;
        return temp;
     }

     operator * (const complexType &a, const complexType &b)
     {
        complexType temp;
        temp.realPart = a.realPart * b.realPart - a.imaginaryPart * b.imaginaryPart;
        temp.imaginaryPart = a.realPart * b.imaginaryPart + a.imaginaryPart *            b.realpart;
       return temp;
       }


       operator / (const complexType &a, const complexType &b)
      {
       complexType temp;
        temp.realPart = (a.realPart * b.realPart + a.imaginaryPart * b.imaginaryPart) /           (pow(b.realPart,2) + pow(b.imaginaryPart,2));
          temp.imaginaryPart = (-a.realPart * b.imaginaryPart +                      a.imaginaryPart*b.realPart) / (pow(b.realPart,2) + pow(b.imaginaryPart,2));
        return temp;
       }

       ostream & operator << (ostream &os, const complexType &a)
       {
         os << "(" << a.realPart << " ," << a.imaginaryPart << "i)";
         return os;
       }

         istream & operator >> (istream &is, const complexType &a)
        {
        char ch, ch2, ch3;
        is >> ch;
        is >> a.realPart;
        is >> ch2;
        is >> a.imaginaryPart;
        is >> ch3;
        return is;
        }

我非常感谢任何有关代码和格式的帮助,因为这是我的第一个问题。提前致谢。

最佳答案

您忘记在运算符定义前添加返回类型 (complexType)。因此返回类型默认为 int 并且与友元声明不匹配。

此外,您的 setComplextType 定义没有类前缀 (complexType::) 并且具有错误的参数类型,但这不应导致错误消息你正在看到。

关于c++ - 类中的运算符重载会导致范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061537/

相关文章:

c++ - 名称查找和运算符重载如何工作?

c++ - 关于一段带有模板、转换运算符和复制构造函数的代码的问题

c++ - 为什么带变量的左移与带常量的左移产生不同的结果?

c++ - 无法推导模板参数

c++ - 渲染器效率

c# - 如何为 INotifyPropertyChanged 接口(interface)创建自己的 PropertyChangedEventArgs?

ruby-on-rails - Ruby -::in 类名

c++ - 我试图在 char 数组的中间添加一个空格。不确定如何正确移动它

c++ - 如果通过new创建对象,成员函数的局部变量在哪里创建?

delphi - Delphi 中的运算符重载