Перегрузка оператора в классе приводит к ошибке области

Я пытаюсь сделать класс для того, чтобы перегрузить +,-,*, /, <<, >>, а также == для того, чтобы работать для комплексных чисел. При попытке устранения неполадок, я получаю несколько сообщений об ошибках, говорящих:

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

Я был бы очень признателен за любую помощь, как с кодом, так и с форматированием, так как это мой первый вопрос. Заранее спасибо.

2

Решение

Вы забыли добавить префикс вашего определения оператора к типу возврата (complexType). Таким образом, тип возвращаемого значения по умолчанию int и не соответствует объявлению друга.

Кроме того, ваше определение setComplextType не имеет префикса класса (complexType::) и имеет неправильные типы параметров, но это не должно вызывать сообщение об ошибке, которое вы видите.

4

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector