few errors when compiling one of them is the inverse function what to return

This is a discussion on few errors when compiling one of them is the inverse function what to return within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cmath> using namespace std; class rational { public: rational(); rational add(rational); rational sub(rational); rational mul(rational); rational ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    few errors when compiling one of them is the inverse function what to return

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    class rational
    {
    public:
                  
           rational();
           rational add(rational);
           rational sub(rational);
           rational mul(rational);
           rational div(rational);
           rational inv(rational);
           int getdenominator();
           int getnumerator();
           void print(rational);
    private:
            int numerator;
            int denominator;
    
            };
            
            void rational::print(rational)//make sure it is right
            { 
       
            cout << numerator << "/" << denominator << endl;
            }
    
            rational rational::inv(rational b)
            {
             return rational.reciprocal(b.getdenominator(),b.getnumerator());//added .  not sure if it is right
            }
             rational rational(int num ,int den )//i added rational the class name in t he front
            {
            int numerator=num;
             int denominator=den;
            } 
              rational rational::add(rational b)
            {
        {
            int num1 = (this.numerator * b.denominator) + (b.numerator * this.denominator); // cross multily and add
            int    num2 = this.denominator * b.denominator; // multiply the denominators to make them equivlent
              return rational answer(num1,num2);
            }
          rational rational::sub(rational b)
          {
                    int num1 = (this.numerator * b.denominator) - (b.numerator * this.denominator);// cross multiply and subtact
            int num2 = this.denominator * b.denominator;
        
            return rational answer(num1,num2);
          }
          rational rational::mul(rational b)
          {
              int num1 = this.numerator * b.numerator; // multiplys straight accross
            int    num2 = this.denominator * b.denominator;
            
            return rational answer (num1,num2);      
          }
          rational rational::div(rational b) 
          {
                   rational reciprocal=inv(b);
                   int num1=numerator*reciprocal.numerator;
                   int num2=denominator*reciprocal.denominator;
           /* int num1 = this.numerator * b.denomerator; //multiplys the inverse of 2nd fraction object to divide
            int num2 = this.denomerator * b.numerator;
            */
            return rational answer (num1, num2);
                   
          } 
          
         
     int main()
          {
            rational c(4,2);
            rational d(8,4);
            rational x;
    
       a.print();
       cout << " addition ";
       b.print();
       x = c.add(d);
       cout << " = ";
       x.print();
       cout << '\n';
       x.print();
       cout << " = ";
       cout << "\n\n";
    
       c.print();
       cout << " subtraction ";
       d.print();
       x = c.sub(d);
       cout << " = ";
       x.print();
       cout << '\n';
       x.print();
       cout << " = ";
       cout << "\n\n";
    
       c.print();
       cout << " multiplication ";
       d.print();
       x = c.mul(d);
       cout << " = ";
       x.print();
       cout << '\n';
       x.print();
       cout << " = ";
       cout << "\n\n";
    
       c.printRational();
       cout << " division ";
       d.printRational();
       x = c.div(d);
       cout << " = ";
       x.printRational();
       cout << '\n';
       x.printRational();
       cout << " = ";
       cout << endl;
       system("pause");
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I see some comments in the code, but none of them seem to indicate what the "errors when compiling" are.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    This
    Code:
    rational rational::inv(rational b)
            {
             return rational.reciprocal(b.getdenominator(),b.getnumerator());//added .  not sure if it is right
            }
    I guess?

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    You need to look at the errors the compiler gives you and start with the first one.

    I can tell that you're unfamiliar with C++ syntax. If you are new, you shouldn't be writing this much code at a time.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by NeonBlack View Post
    You need to look at the errors the compiler gives you and start with the first one.

    I can tell that you're unfamiliar with C++ syntax. If you are new, you shouldn't be writing this much code at a time.
    I strongly agree with both of those. Write one function, get it to compile and produce the correct output, and then start the next one.

    For now, how about commenting out add, sub, mul, div, and inv, and just get it printing out the initial value of c correctly. You've done at least two different things wrong in your constructor, so start doing some research about what the proper syntax for that is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21