2

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

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

对于代码和格式方面的任何帮助,我将不胜感激,因为这是我的第一个问题。提前致谢。

4

1 回答 1

4

您忘记在运算符定义前加上返回类型 ( complexType)。所以返回类型默认为int并且不匹配朋友声明。

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

于 2012-10-25T04:31:00.660 回答