C++ 运算符重载(使用binaray friend函数)类没有成员,且成员不可访问

C++ 运算符重载(使用binaray friend函数)类没有成员,且成员不可访问,c++,function,overloading,operator-keyword,friend,C++,Function,Overloading,Operator Keyword,Friend,在youtube上学习运算符重载的教程时,我在修复运算符重载(使用friend函数)错误消息时遇到问题。收到的消息是关于,类复合体没有成员“operator+”和类 第7行声明的“Complex::real”不可访问 //链接到我正在努力学习的教程 //错误消息从这里开始:// #include "stdafx.h" #include <iostream> using namespace std; class Complex { int real, imag; publi

在youtube上学习运算符重载的教程时,我在修复运算符重载(使用friend函数)错误消息时遇到问题。收到的消息是关于,类复合体没有成员“operator+”和类 第7行声明的“Complex::real”不可访问

//链接到我正在努力学习的教程

//错误消息从这里开始://

#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;
}
#包括“stdafx.h”
#包括
使用名称空间std;
阶级情结
{
int-real,imag;
公众:
无效读取();
void show();
友元复数运算符+(复数,复数);//友元函数声明
};
void Complex::read()
{
真实的;
cout>imag;
}
void Complex::show()
{

cout朋友
不是成员。因此,更改此行:

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


声明为
friend
的函数不是成员函数。为什么要将
operator+
函数声明两次,一次在顶部,另一次在
main
的正上方?有些关联,如果您检查,例如,特别是关于使用
运算符+=
执行。
Complex operator + (Complex c1, Complex c2)