-1
friend 함수와 생성자를 사용하여 값을 초기화했지만 complex1 :: complex1()에 대한 정의되지 않은 참조가 발생하여 2 개의 복소수를 추가하려고합니다.friend 함수를 사용하여 복소수 정보
#include<iostream>
using namespace std;
class complex1
{
float real,img;
public:
complex1();
complex1(float a,float b)
{
real=a;
img=b;
}
friend complex1 sum(complex1,complex1);
friend void display(complex1);
};
complex1 sum(complex1 c1,complex1 c2)
{
complex1 c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}
void display(complex1 c)
{
cout<<c.real<<"+j"<<c.img;
}
int main()
{
complex1 c1(100.9,200.9);
complex1 c2(50.9,50.9);
complex1 c=sum(c1,c2);
display(c); //display and sum is given directly because it is friend
return 0;
}
'complex1의 C = 합계 (C1, C2)'는'더 구현이없는 기본 생성자와 c'를 구축하는 ('complex1() ; '수업에서). 그런 다음, 함수'sum'에서 이것을 사용하고 있습니다. 또한, 정의되지 않은'complex1' 타입을 사용하여'operator ='를 사용하고 있습니다. – Fureeish
@Fureeish'operator ='는별로 중요하지 않습니다. 이것은 컴파일러에 의해 만들어지며 복사 생성자 나 소멸자를 정의 할 필요가 없으므로 3의 규칙을 위반하지 않습니다. –
@FantasticMrFox Correct. 컨텍스트에 대한 설명을 그대로 남겨 두겠습니다. – Fureeish