Expression must have a class type?

Hi forum,

I reckon something is wrong with my reduce function, because when I remove the .reduce() from line 111, the keyword this becomes no longer red-underlined. Perhaps I am mistaken and something is wrong with my logic? Please advise,

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//header<--------------------

#ifndef SICT_FRACTION_H
#define SICT_FRACTION_H

// TODO: create namespace
namespace sict
{
	// TODO: define the Fraction class
	class Fraction {
		// TODO: declare instance variables 
		int numerator;
		int denominator;
		int result;
		// TODO: declare private member functions
		int max() const;
		int min() const;
		void reduce();
		int gcd() const;
	public:
		// TODO: declare public member functions
		Fraction();
		Fraction(const int num, const int den);
		bool isEmpty();
		void display();
		// TODO: declare the + operator overload
		Fraction operator+(const Fraction& rhs) const;
	};
}
#endif

//implementation file<-------------------------

#include <iostream>
// TODO: continue the namespace
namespace sict
{
	// TODO: implement the default constructor
	Fraction::Fraction()
	{
		numerator = 0;
		denominator = 0;
	}
	// TODO: implement the two-argument constructor
	Fraction::Fraction(const int num, const int den)
	{
		if (0 < num && 0 < den)
		{
			numerator = num;
			denominator = den;
		}
		else
		{
			Fraction::Fraction();
		}
	}
	// TODO: implement the max query
	int Fraction::max() const
	{
		if (numerator > denominator)
		{
			return numerator;
		}
		else
		{
			return denominator;
		}
	}
	// TODO: implement the min query
	int Fraction::min() const
	{
		if (numerator > denominator)
		{
			return denominator;
		}
		else
		{
			return numerator;
		}
	}
	// gcd returns the greatest common divisor of num and denom
	//
	int Fraction::gcd() const
	{
		int mn = min();  // min of numerator and denominator
		int mx = max();  // max of numerator and denominator
		int g_c_d = 1;
		bool found = false;

		for (int x = mn; !found && x > 0; --x) { // from mn decrement until divisor found
			if (mx % x == 0 && mn % x == 0) {
				found = true;
				g_c_d = x;
			}
		}
		return g_c_d;
	}


	// 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;
		}

	}
	// TODO: implement the isEmpty query

	bool Fraction::isEmpty()
	{
		if (0 == numerator || 0 == denominator)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// TODO: implement the + operator
	Fraction Fraction::operator+(const Fraction& rhs) const
	{
		Fraction sum;
		sum.numerator = (this->numerator * rhs.denominator) + (this->denominator * rhs.numerator);
		sum.denominator = this->denominator * rhs.denominator;
		return sum.reduce();
	}
}

//main<--------------------

using namespace std;
using namespace sict;

int main() {
    cout << "------------------------------" << endl;
    cout << "Fraction Class Test:" << endl;
    cout << "------------------------------" << endl;
    
    sict::Fraction a;
    cout << "Fraction a; // ";
    cout << "a = ";
    a.display();
    cout << endl;

    Fraction b(1, 3);
    cout << "Fraction b(1, 3); // ";
    cout << "b = ";
    b.display();
    cout << endl;
    
    Fraction c(-5, 15);
    cout << "Fraction c(-5, 15); //";
    cout << " c = ";
    c.display();
    cout << endl;

    Fraction d(2, 4);
    cout << "Fraction d(2, 4); //";
    cout << " d = ";
    d.display();
    cout << endl;

    Fraction e(8, 4);
    cout << "Fraction e(8, 4); //";
    cout << " e = ";
    e.display();
    cout << endl;

    cout << "a + b equals ";
    (a + b).display();
    cout << endl;

    cout << "b + d equals ";
    (b + d).display();
    cout << endl;
   
    return 0;
}
Last edited on
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
Last edited on
Topic archived. No new replies allowed.