• Advertisement
Sign in to follow this  

Overloading operators error [C++]

This topic is 3001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, so I'm making a Rational numbers class for a homework assignment and I'm not asking for answers as to what to do but maybe someone can explain why I'm getting the following error:
Error	1	error C2662: 'Rational::Getp' : cannot convert 'this' pointer from 'const Rational' to 'Rational &'	c:\users\daniel caban\documents\visual studio 2008\projects\pa2\pa2\rational.cpp	25

Here is what the code looks like:
const Rational operator +(const Rational& r1, const Rational& r2)
{
	int a = r1.Getp();
	int b = r1.Getq();

	int c = r2.Getp();
	int d = r2.Getq();

	int numerator = (a * d) + (b * c);
	int denominator = b * d;

	return Rational(numerator, denominator);
}

(i just previewed it and the plus sign didn't show in the source tags. there is a plus sign to the left of the (const Ratio.....)) Oh and if the math is wrong please ignore it for now. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
It looks like your Getp() isn't a const member function. Make it one.

Share this post


Link to post
Share on other sites
I tried changing the Getp() and Getq() member functions to const but it still gives me the same error (I get that error for Getq() as well). I'll post the class so you can see what I have now.

Rational.h
#pragma once
#include <string>

using namespace std;

class Rational
{
private:
int p, q;
public:
Rational(int P, int Q);
~Rational(void);
string ToString();
const int Getp();
const int Getq();
};

const Rational operator +(const Rational& r1, const Rational& r2);
const Rational operator -(const Rational& r1, const Rational& r2);
const Rational operator *(const Rational& r1, const Rational& r2);
const Rational operator /(const Rational& r1, const Rational& r2);



Rational.cpp

#include "Rational.h"

Rational::Rational(int P, int Q)
{
p = P;
q = Q;
}

Rational::~Rational(void)
{
}

const int Rational::Getp()
{
return p;
}

const int Rational::Getq()
{
return q;
}

const Rational operator +(const Rational& r1, const Rational& r2)
{
int a = r1.Getp();
int b = r1.Getq();

int c = r2.Getp();
int d = r2.Getq();

int numerator = (a * d) + (b * c);
int denominator = b * d;

return Rational(numerator, denominator);
}

const Rational operator -(const Rational& r1, const Rational& r2)
{
int a = r1.Getp();
int b = r1.Getq();

int c = r2.Getp();
int d = r2.Getq();

int numerator = (a * d) - (b * c);
int denominator = (b * d);

return Rational(numerator, denominator);
}

const Rational operator *(const Rational& r1, const Rational& r2)
{
int a = r1.Getp();
int b = r1.Getq();

int c = r2.Getp();
int d = r2.Getq();

int numerator = (a * c);
int denominator = (b * d);

return Rational(numerator, denominator);
}

const Rational operator /(const Rational& r1, const Rational& r2)
{
int a = r1.Getp();
int b = r1.Getq();

int c = r2.Getp();
int d = r2.Getq();

int numerator = (a * d);
int denominator = (b * c);

return Rational(numerator, denominator);
}
string Rational::ToString()
{
string s = p + "/" + q;
return s;
}



(again for some reason the plus signs aren't showing up in the preview)

Share this post


Link to post
Share on other sites
Oh wow okay that fixed everything. Thanks a lot.

(Time to google what const does when it's after a member function declaration.)

Edit:
Just saw the link you posted, thanks :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement