Why oh why don't you tell us what the error associated with the "red underlining" was? Part of programming is learning how to decipher error messages. We can't help translate it if we don't see it.
But anyway, your error is easy enough to catch pretty quickly, in this case.
|
std::cout << this->numerator.reduce() << "/" << this->denominator.reduce() << std::endl;
|
numerator and denominator are ints... they dont have a "reduce" function.
And your code doesn't make logical sense, how can you only reduce the numerator separate from the denominator?
just do
1 2
|
this->reduce();
std::cout << this->numerator << "/" << this->denominator << std::endl;
|
________________________________________
Furthermore:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// TODO: implement the reduce modifier
void Fraction::reduce()
{
numerator = numerator / Fraction::gcd();
denominator = denominator / Fraction::gcd();
}
// TODO: implement the display query
void Fraction::display()
{
if (!Fraction::isEmpty() && this->denominator != 1)
{
std::cout << this->numerator.reduce() << "/" << this->denominator.reduce() << std::endl;
}
else if (this->denominator == 1)
{
std::cout << this->numerator << std::endl;
}
else
{
std::cout << "no fraction stored" << std::endl;
}
}
|
reduce()
is a void function -- it doesn't return anything. In your current design, your reduce function is only mutating the Fraction. There's nothing to print.
If you want to be able to print out
std::cout << this->reduce() << std::endl;
reduce() needs to return a reference to Fraction, and you need to overload the << operator for Fractions.
edit: Fixed code