I get this error even though I overloaded the << operator.
My prototype is as follows, placed within the Fraction class header file.
friend std::ostream& operator << (std::ostream&, Fraction&);
My overloaded function is as follows:
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;
}
This is the function where I get the error:
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;
}
}
}
I really have no idea what could be wrong, I don't even know where to start. I'm a newbie at C++ and Object-Oriented Programming, so if anyone can pinpoint the issue, that would be a massive help.
If you read the error message carefully, it says you are trying to pass a const Fraction
object to your operator<<
, which takes a Fraction&
reference instead. A non-const reference cannot bind to a const object.
The elements of your fr[]
array are const Fraction
objects. In order to print them, you need to add const
to your operator declaration (which you should do anyway, as output streams should always act on const
objects):
friend std::ostream& operator << (std::ostream&, const Fraction&)
// ^^^^^ HERE
Alternatively:
friend std::ostream& operator << (std::ostream&, Fraction const &)
// ^^^^^ HERE