Not sure how to fix my code.

I have to write code around my instructors code and I cannot make changes to his code. So I am not sure how i can fix my error message.
And the error shows up in his file.
Line 27
error: could not convert 'f2.fraction::isGreaterThan(f3)' to 'bool'

what can i do?

This is his code I cannot make changes too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8); //calling a parameterized class constructor
    fraction f2(2,3); //calling a parameterized class constructor
    fraction result;  //calling a default class constructor
    fraction f3; //calling a default class constructor

    cout << "The result starts off at ";
    result.print(); //calling an observer function
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    cout << " is ";
    result = f1.MultipliedBy(f2); //a class binary operation - function
    result.print();
    cout << endl;

    f3 = result; //assignment

     if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
        f2.print();
        cout <<" is greater than ";
        f3.print();
        cout<<endl;
    } else {
        f2.print();
        cout <<" is less than ";
        f3.print();
        cout<<endl;
    }




Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED

class fraction
{
public:
    fraction (); // default constructor
    fraction (int,int);
    void print() const;
    fraction MultipliedBy(fraction) const;
    fraction isGreaterThan(fraction) const;
    fraction AddedTo(fraction) const;
    fraction Subtract(fraction) const;
    fraction isEqualTo(fraction) const;
    fraction DividedBy(fraction) const;
private:
    int numerator;
    int denominator;
};

#endif // FRACTION_H_INCLUDED



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include "fraction.h"

using namespace std;

    fraction::fraction ()// default constructor
    {
        numerator=0;
        denominator=1;
    }

    fraction::fraction (int newNumerator,int newDenominator)
    {
        denominator=newDenominator;
        numerator=newNumerator;
    }

    void fraction::print() const
    {
        cout<<numerator<<"/"<<denominator;
    }

    fraction fraction::MultipliedBy(fraction f) const
    {
        int temp,m,n;
        /*not sure where but somewhere here is
        where i think i need add Euclid's Algoritm*/
        f.denominator*=denominator;
        f.numerator*=numerator;
       // here is Euclid's Algorithm to reduce the fractions
       //http://simple.wikipedia.org/wiki/Euclidean_algorithm
        m=f.numerator;
        n=f.denominator;
                 if(m < n)
                {
                temp = m;
                m = n;
                n = temp;
                }
        while(n != 0)
                {
                temp = m % n;
                m = n;
                n = temp;
                }
        f.denominator=f.denominator/m;
        f.numerator=f.numerator/m;

        return f;

}



    fraction fraction::isGreaterThan(fraction f) const
    {
// i know i need to put code in here but the program should still run //eventhough this blank.        

    }
fraction bool fraction::isGreaterThan(fraction f) const
Should return bool, not fraction.

Or go the messy way, and provide conversion operators to your class.
fraction::operator bool() // God help you.

Or go the sinful way and use reinterpret_cast<bool>.

Edit: tags.
Last edited on
thank you idk why i could not see that lol.
Topic archived. No new replies allowed.