Recebo esse erro mesmo tendo sobrecarregado o operador <<.
Meu protótipo é o seguinte, colocado dentro do arquivo de cabeçalho da classe Fraction.
friend std::ostream& operator << (std::ostream&, Fraction&);
Minha função sobrecarregada é a seguinte:
std::ostream& operator << (std::ostream &output, Fraction &f1)
{
if(f1.denominator == 1){
output << f1.numerator;
}
if(abs(f1.numerator) >= f1.denominator){
if(f1.numerator % f1.denominator == 0){
output << f1.numerator / f1.denominator;
}
else {
Fraction f2(f1.numerator % f1.denominator, f1.denominator);
f2.simplify();
output << f1.numerator / f1.denominator << "+" << f2.numerator << "/" << f2.denominator;
}
}
else {
output << f1.numerator << "/" << f1.denominator;
}
return output;
}
Esta é a função onde recebo o erro:
void BasicTest()
{
cout << "***********************************************************************\n";
cout << "* Basic Test: Testing member constructor, simplify() and nonmember *\n";
cout << "* friend ostream << operator for basic Fraction object creation & *\n";
cout << "* printing(Fractions should be in reduced form, and as mixed numbers.)*\n";
cout << "***********************************************************************\n";
const Fraction fr[] = {Fraction(4, 8), Fraction(-15,21),
Fraction(10), Fraction(12, -3),
Fraction(), Fraction(28, 6), Fraction(0, 12)};
for (int i = 0; i < 7; i++){
cout << "Fraction [" << i <<"] = " << fr[i] << endl; // This is where I get the error
}
cout << "\n***********************************************************************\n";
cout << "* Basic Test: Testing simplify() and nonmember friend istream >> and *\n";
cout << "* ostream << operators for reading and display of Fraction objects *\n";
cout << "* from data file *\n";
cout << "***********************************************************************\n";
string fileName;
cout << "Enter file name with fraction data to read: ";
cin >> fileName;
ifstream in(fileName);
while(!in)
{
cin.ignore(200, '\n');
cout << fileName << " not found!" <<endl;
cout << "Make sure the fraction data file to read is in the project folder." << endl;
cout << "Enter file name with fraction data to read: ";
cin >> fileName;
in.open(fileName);
}
while (!eof(in)) {
Fraction f;
if(in.peek() == '/' || in.peek() == '*' || isalpha(in.peek()) ){
in.ignore(200, '\n'); //skip this line, it's a comment
} else {
in >> f;
cout << "Read Fraction = " << f << endl;
}
}
}
Realmente não tenho ideia do que possa estar errado, nem sei por onde começar. Sou iniciante em C++ e Programação Orientada a Objetos, então se alguém puder identificar o problema, seria de grande ajuda.