【问题标题】:Operator Overloading in a class, results in a scope error类中的运算符重载,导致范围错误
【发布时间】:2012-10-25 04:23:37
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 另外operator == 需要删除const 并采用第二个参数。
    • 其实这个函数应该是一个类函数。我会采取另一种方式:不要做朋友的事情,将其保留为 const 并相应地实现它。对于两边都有complexType 的其他运营商也可以这样说......
    • 是的,我同意,在定义 LHS 上具有类类型的运算符重载时避免使用友元语法,这似乎是 OP 想要使用的模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2014-03-26
    • 2015-12-24
    • 2013-11-23
    • 2020-06-09
    • 2018-01-01
    相关资源
    最近更新 更多