Fraction

I created a fraction class but seem to have run into an error. What seems to be the problem?

#include <iostream>
#include <iomanip>
using namespace std;

class Fraction{
public:
// Κατασκευαστές
Fraction(); // Εξ' ορισμού κατασκευαστής
Fraction(int n, int d);

// Συναρτήσεις πρόσβασης (accessors)
int getNum();
int getDen();

// Συναρτήσεις χειρισμού (modifiers)
void setNum(int n);
void setDen(int d);

void display(); // cout να τυπώσει το κλάσμα!!

private:
int num; // αριθμητής
int den; // παρονομαστής
};

int Fraction::setNum(){

}


int main() {
Fraction f(1, 2); // 1/2
f.display(); // --> 1/2

f.setDen(4); // 1/4
f.display(); // --> 1/4

f.setNum(2); // 2/4
f.display(); // --> 2/4

return 0;
}

if anyone can help that would be great. Thank you in advance
but seem to have run into an error. What seems to be the problem?

Well, that wasn't the most helpful of descriptions!

You have declared, but not defined (i.e. written) your member functions. Despite the AI in your user name they aren't capable of writing themselves.


Please put any code samples inside code tags.
Last edited on
sorry for the incovinience lol im still new. the error was:
error: prototype for 'int Fraction::setNum()' does not match any in class 'Fraction'
int Fraction::setNum(){ and
error: candidate is: void Fraction::setNum(int)
void setNum(int n);
That will only be the first error.

You have declared a "setter" with one argument and (correctly) returning nothing:
void setNum(int n);
but "started" writing one with no arguments and (unnecessarily) claiming to return an int:
int Fraction::setNum()

They don't match.

After that, you are calling a lot of functions that you have yet to write the content of.

A better way to initialise an object of a class like this is through its constructor.
Last edited on
Ok thank you a lot for the pointers.Appreciate it
When posting please, please use code tags so that the code is readable!

Perhaps:

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
#include <iostream>

class Fraction {
public:
	Fraction() {}
	Fraction(int n, int d) : num(n), den(d) {}

	int getNum() const { return num; }
	int getDen() const { return den; }

	void setNum(int n) { num = n; }
	void setDen(int d) { den = d; }

	void display() const { std::cout << num << '/' << den << '\n'; }

private:
	int num {};
	int den {1};
};

int main() {
	Fraction f(1, 2); // 1/2

	f.display(); // --> 1/2

	f.setDen(4); // 1/4
	f.display(); // --> 1/4

	f.setNum(2); // 2/4
	f.display(); // --> 2/4
}

If you search the forum for "fraction" and "rational" you'll find several threads about this exact class
Topic archived. No new replies allowed.