+
、、、、、、をオーバーロードするために-
、そして複素数を処理する*
ため/
にクラスを作成しようとしています。トラブルシューティングを試みているときに、次のような複数のエラーメッセージが表示されます。<<
>>
==
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;
}
これが私の最初の質問なので、コードとフォーマットの両方で助けていただければ幸いです。前もって感謝します。