Em đang làm bài tập về nạp chồng toán tử thì gặp lỗi
Error (active) no operator ">>" matches these operands
Error (active) no operator "<<" matches these operands
Error C2679 binary '>>': no operator found which takes a right-hand operand
of type 'Fraction' (or there is no acceptable conversion)
Error C2679 binary '<<': no operator found which takes a right-hand operand
of type 'Fraction' (or there is no acceptable conversion)
.Em đã google nhưng cho ra các kết quả không như mong đợi. Mong các anh / chị giúp đỡ em với 
Đây là code của em:
File: fraction.h
#ifndef _FRACTION_
#define _FRACTION_
#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(int, int);
int GetNumerator();
int GetDenominator();
private:
int Numerator;
int Denominator;
};
#endif // _FRACTION_
File: fraction.cpp
#include "fraction.h"
Fraction::Fraction(int _numerator = 1, int _denominator = 2) {
this->Numerator = _numerator;
this->Denominator = _denominator;
}
int Fraction::GetNumerator() {
return this->Numerator;
}
int Fraction::GetDenominator() {
return this->Denominator;
}
std::istream &operator >> (std::istream &in, Fraction &_fraction) {
int _numerator = 0;
cout << "\n Enter the numerator: ";
in >> _numerator;
int _denominator = 0;
do {
cout << " Enter the denominator: ";
in >> _denominator;
} while (_denominator == 0);
_fraction = Fraction(_numerator, _denominator);
return in;
}
std::ostream &operator<<(std::ostream &out, Fraction _fraction) {
return out << _fraction.GetNumerator() << " / " << _fraction.GetDenominator() << endl;
}
File: main.cpp
#include "fraction.h"
int main() {
Fraction mFraction(2, 3);
cin >> mFraction;
cout << mFraction;
system("pause");
return 0;
}
Thanks a lot !
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?