C++ class help needed

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

andy2003

Thread Starter
Joined
Oct 12, 2003
Messages
1
I am developing a rational number class in C++... the class is almost entirley completed however I have one problem... it takes a rational in the for r1(intn, intd) and overloads operators etc. Everything works between rationals the problem is when you attempt to use a rational with an int. I have it working for (rational + int) but does not work for (int + rational). I know I need to declare these options globally which I thought I did but it did not work.... right now I have a rational.h file, a rational.cpp file, and a rationaltestdriver.cpp file.

To deal with the (int + rational) issue I put the following in the rational.cpp file (along with all the other class funcions, however my rationaltestdriver.cpp file does not seem to see these... How do I get these functions to work with the entire class? Any help would be great.. thanks!



Rational operator+ (int left_operand, Rational right_operand);
Rational operator- (int left_operand, Rational right_operand);
Rational operator* (int left_operand, Rational right_operand);
Rational operator/ (int left_operand, Rational right_operand);

Rational operator+ (int left_operand, Rational right_operand)
{
Rational new_operand(left_operand, 1);
return new_operand + right_operand;
}
Rational operator- (int left_operand, Rational right_operand)
{
Rational new_operand(left_operand, 1);
return new_operand - right_operand;
}
Rational operator* (int left_operand, Rational right_operand)
{
Rational new_operand(left_operand, 1);
return new_operand * right_operand;
}
Rational operator/ (int left_operand, Rational right_operand)
{
Rational new_operand(left_operand, 1);
return new_operand / right_operand;
}
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top