2012-12-15 6 views
2

두 분수를 추가 할 오버로드 된 친구 함수를 만들려고합니다. 지금까지 나는 다음과 같은 해결책을 가지고 있지만 17 번과 19 번 줄에 오류가 있습니다. 문제는 매개 변수 (분수 amount1, 분수 2)와 관련이 있다고 생각하지만 정확히 내 손가락을 넣을 수는 없습니다. 누구든지 도와 줄 수 있습니까? 이것은 비주얼 스튜디오에 2010C++ 과부하 + 연산자 친구 함수

#include <iostream> 

using namespace std; 

class fraction 
{ 
public: 
fraction(); //constructor for initialize 
void read(); //read 3 int from keyboard for the fraction 
void print(); //print the fraction on screen 
friend fraction operator +(fraction, fraction); //fraction add 
private: 
int integral, numerator, denominator; 
} 


int main(){ 

return 0; 
} 

fraction operator +(fraction amount1, fraction amount2) 
{ 
fraction temp; 
temp.integral = amount1.integral + amount2.integral; 
temp.numerator = amount1.numerator + amount2.numerator; 
temp.denominator = amount1.denominator + amount2.denominator; 
return temp; 
} 


void fraction::read() 
{ 
cin >> integral >> numerator >> denominator; 
} 

편집을 컴파일 할 필요가 : 나는

friend fraction operator +(const fraction&, const fraction&); //fraction add 

fraction operator +(const fraction& amount1, const fraction& amount2) 
{ 
fraction temp; 
temp.integral = amount1.integral + amount2.integral; 
temp.numerator = (amount1.numerator)*(amount2.denominator) + (amount2.numerator)*(amount1.denominator); 
temp.denominator = (amount1.denominator)*(amount2.denominator); 
return temp; 
} 

내뿐만 아니라 알고리즘을 수정 한 단 분자이 I 출력 0을 테스트에 및 분모 변수. 아무도 이것으로 도울 수 있습니까? 클래스 블록의 외부에서 정의 경우

+1

참조 (http://stackoverflow.com/questions/4421706/operator-overloading)의 서명을 변경해야합니다 각 연산자에 과부하가 걸립니다. 예를 들어,'operator +'는 보통'operator + ='로 구현됩니다. – chris

+1

'operator +'가'const' 참조를 원할 수도 있습니다. –

+1

클래스 정의 후';'가 누락되었습니다. 그렇지 않으면, 당신의 코드는 완벽하게 괜찮습니다 ("compilability"에 관한 한). 많은 것을 개선 할 수는 있지만';'외에는 오류가 없습니다. – AnT

답변

0

이 변화의 copule 후 컴파일 :

  • 당신은 클래스 이후에 세미콜론을 추가해야합니다 정의 및
  • 선언 한 생성자를 정의해야합니다. 즉,

fraction(); //constructor for initialize 

가 본체 인라인, 또는 별도의 파일 중 하나를 정의한다

이 constructor.

또한 방법에 대한 좋은 조언을 [여기] 당신의 operator

friend fraction operator +(const fraction&, const fraction&) 
+0

다음은 [ideone 링크] (http://ideone.com/OXL95l)입니다. – dasblinkenlight

+0

교환 원의 서명을 변경하는 이유는 무엇입니까? – user1905552

+0

@ user1905552 서명을 변경하면 연산자가 더 많은 컨텍스트에서 작업 할 수 있습니다. 예를 들어 피연산자 중 하나 또는 둘 모두가 'const'로 선언 된 경우입니다. – dasblinkenlight

-1

당신은 연산자 앞에 클래스 이름이 필요합니다

fraction fraction::operator +(fraction amount1, fraction amount2); 
+2

아니요. -1이 아니지만 클래스 정의 내에서 친구로 선언 된 함수는 * namespace scope * 함수를 참조합니다. 그들은 반원이 아니다. – AnT

+3

이것은 자유 함수로 멤버가 아닌'class'의 친구입니다. – Johnsyweb

0
#include <iostream> 

using namespace std; 

class fraction 
{ 
public: 
//fraction(); //constructor for initialize 
void read(); //read 3 int from keyboard for the fraction 
void print(); //print the fraction on screen 
friend fraction operator +(fraction, fraction); //fraction add 
private: 
int integral, numerator, denominator; 
}; 


int main(){ 
fraction f1, f2, f3; 
cout<<"\n Enter first fraction: "; 
f1.read(); 
cout<<"\n Enter second fraction: "; 
f2.read(); 
cout<<"\n result: "; 

f3 = f1 + f2; 
f3.print(); 
return 0; 
} 

fraction operator +(fraction amount1, fraction amount2) 
{ 
fraction temp; 
temp.integral = amount1.integral + amount2.integral; 
temp.numerator = amount1.numerator + amount2.numerator; 
temp.denominator = amount1.denominator + amount2.denominator; 
return temp; 
} 


void fraction::read() 
{ 
cin >> integral >> numerator >> denominator; 
} 

void fraction::print() 
{ 
cout << integral<<" "<< numerator <<" "<< denominator <<std::endl; 
}