Перегрузка оператора (с использованием функции-друга binaray) не имеет члена, а член недоступен - PullRequest
0 голосов
/ 05 мая 2018

При выполнении учебника YouTube по перегрузке операторов у меня возникают проблемы с исправлением сообщения об ошибке перегрузки операторов (с помощью функции друга). Полученное сообщение было о, класс Complex не имеет члена "operator +" и класса "Complex :: real", объявленный в строке 7, недоступен.

// ссылка на учебник, с которым я борюсь https://www.youtube.com/watch?v=AlCYu_mc-T8

// здесь появляется сообщение об ошибке: //

#include "stdafx.h"
#include <iostream>
using namespace std;

class Complex
{
    int real, imag;
public:
    void read();
    void show();
    friend Complex operator+ (Complex , Complex); // Friend function declaration
};

void Complex::read()
{
    cout << "Enter real value: ";
    cin >> real;
    cout << "Enter imaginary value: ";
    cin >> imag;
}

void Complex::show()
{
    cout << real;
    if (imag < 0)
        cout << "-i";
    else
        cout << "+i";
    cout << abs(imag) << endl;
}

Complex Complex::operator+(Complex c1, Complex c2)
{
    Complex temp;
    temp.real = c1.real + c2.real;
    temp.imag = c1.imag + c2.imag;
    return temp;
}

int main()
{
    Complex c1, c2, c3;
    c1.read();
    c2.read();
    c3 = c1 + c2; // invokes operator + (Complex, Complex)
    cout << "Addition of c1 and c2 = ";
    c3.show();
    return 0;
}

1 Ответ

0 голосов
/ 05 мая 2018

friend не является участником. Следовательно, измените эту строку:

Complex Complex::operator+(Complex c1, Complex c2)

до:

Complex operator + (Complex c1, Complex c2)
...