1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ class help needed

Discussion in 'Software Development' started by andy2003, Oct 12, 2003.

Thread Status:
Not open for further replies.
  1. andy2003

    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;
    }
     
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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/171447

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice