Overloading Arithmetic Operators

So I am making a rational number class. Ive got it most of the way done accept when I use the + operator the program crashes.

So I have a Rational class. All my operators are in the Rational class. My << and >> are friends of Rational class.

1
2
3
4
5
6
7
8
// Here is my + overloaded operator
Rational Rational::operator+(const Rational &rhs){
	Rational temp;
	int cmd = denom * rhs.denom;
	temp.num = (num*rhs.denom)+(rhs.num+denom);
	temp.denom = cmd;
	return temp;
}

When I declare Rational objects a, b , c and try say a=b+c the program stops working. Is this operator wrong or could it be a problem with the = operator?
I just don't know. Any help would be awesome. Thanks
I can post more of the code if needed.
operator+ looks correct. Can you show the definition of Rational?
Last edited on
Yes sir. Here it.
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
class Rational{
    friend istream& operator >>(istream &is,Rational &obj);
    friend ostream& operator <<(ostream &os,const Rational &obj);
public:
	Rational();
	Rational(int newnum);
	Rational(int newnum, int newdenom);
	Rational(string);
	Rational(const Rational &);
	
	Rational &operator=(const Rational &);
	Rational &operator=(const int);
	Rational &operator=(const string);
	Rational operator+(const Rational &);
	Rational operator+(int);
	Rational operator+(string);
	Rational &operator+=(const Rational &);
	Rational &operator+=(int);
	Rational &operator+=(string);
	bool operator==(const Rational &);
	bool operator>(const Rational &);
	Rational operator++();
	Rational operator++(int);
	operator double();
	
private:
	int num;
	int denom;
	string a;
	int *p;
};
int ComputeGCD(int num, int denom);
void Simplify(int &num, int &denom);
void input();
Make sure you have implemented the copy constructor Rational(const Rational &); and the copy assignment operator Rational &operator=(const Rational &); correctly.

You should probably also have a destructor, but I don't think that is the cause of the problem.
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
Ok. So I think the assignment operator is working...
1
2
3
4
5
Rational& Rational::operator=(const Rational &rhs){
	num = rhs.num;
	denom = rhs.denom;
	Simplify(num, denom);
	return *this;


I think it might be a problem with the copy constructor then.
1
2
3
4
5
Rational::Rational(const Rational &obj){
	int *p;
	p = new int;
	*p = *obj.p;
}

See any problems besides that memory leak?
Last edited on
Besides leaking memory, that constructor does nothing.
Edit: it may be dereferencing an invalid pointer too.

┬┐what are your `a' and `p' members for?

> try say a=b+c the program stops working.
debugger, backtrace


By the way, Rational operator+(const Rational &) const;
Last edited on
1
2
3
4
5
6
7
Rational Rational::operator+(const Rational &rhs){
	Rational temp;
	int cmd = denom * rhs.denom;
	temp.num = (num*rhs.denom)+(rhs.num+denom);
	temp.denom = cmd;
	return temp;
}


Shouldn't line 4 read as follows:
temp.num = (num*rhs.denom)+(rhs.num*denom);
That is supposed to be a constructor to copy another object... implemented as
Rational a;
Rational e(a);

p is for the copy constructor.
a is just for the string conversions.

And thanks. Yeah that is supposed to a *.
Last edited on
Limit the scope of your variables.
I don't understand how are you planning to use `p', but it looks that `a' is just an auxiliar.

In the copy constructor that you posted, you are setting a local variable. Your member `p' remains uninitialized (as well as `num' and `denom')
1
2
3
Rational a;
Rational b(a);
Rational c(b); //dereferencing an invalid pointer, possible crash 
Ok I read over the copy constructor material... Just not getting it.
Topic archived. No new replies allowed.