C++ 类不';我没有成员功能
我在尝试正常的朋友功能。我被困在这一点上。这表明Complexnos类没有成员addC++ 类不';我没有成员功能,c++,friend-function,C++,Friend Function,我在尝试正常的朋友功能。我被困在这一点上。这表明Complexnos类没有成员add #include<iostream> using namespace std; class Complexnos { private: int real,img; public: void read() { cout<<"Enter the Real and Imaginary numbers : ";
#include<iostream>
using namespace std;
class Complexnos
{
private:
int real,img;
public:
void read()
{
cout<<"Enter the Real and Imaginary numbers : ";
cin>>real>>img;
}
friend Complexnos add(Complexnos c1, Complexnos c2);
void display()
{
cout<<"The addition of the Complex numbers is : "<<real<<" + "<<img<<"i";
}
};
Complexnos add(Complexnos c1, Complexnos c2)
{
Complexnos c;
c.real = c1.real + c2.real;
c.img = c2.img + c2.img;
return c;
}
int main()
{
Complexnos c1;
Complexnos c2;
Complexnos c3;
c1.read();
c2.read();
c3 = c1.add(c1,c2); <-- Having error here!
c3.display();
return 0;
}
#包括
使用名称空间std;
类复合体
{
私人:
int real,img;
公众:
无效读取()
{
coutreal>>img;
}
友人综合体添加(综合体c1、综合体c2);
无效显示()
{
cout您下面的行声明了一个自由函数,它接受两个参数
friend Complexnos add(Complexnos c1, Complexnos c2);
c3 = c1.add(c2); <-- Having error here!
但是您在这里的用法假定使用一个非自由成员函数,只使用一个参数
friend Complexnos add(Complexnos c1, Complexnos c2);
c3 = c1.add(c2); <-- Having error here!
<代码>朋友>代码>函数是自由函数,不是成员。<代码> >复杂的(CythNOS1 C1,CopyNOS C2);< /COD>从类匹配<代码>复杂(CopyNOS C2)< /C> >从全局空间>代码> Calrase+Real+C2.Realth.< /Case>这是什么独立代码>真正的< /代码>?它来自哪里?C++中没有()Sry伙计们,我的错,我粘贴了错误的代码。我相信这个答案仍然可以解决问题,即使在问题被更改之后。我已经更改了答案,以匹配问题的当前版本。仍然相同Problem@PrathamYadav没问题,它可以编译。