在执行有关运算符重载的youtube教程时,我无法修复运算符重载(使用友元函数)错误消息。收到的消息是关于,类Complex没有成员"运营商+"和班级 "复::真实"在第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;
}
答案 0 :(得分:0)
friend
不是会员。因此,改变这一行:
Complex Complex::operator+(Complex c1, Complex c2)
到:
Complex operator + (Complex c1, Complex c2)