Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:50+00:00 2026-05-28T13:24:50+00:00

greeting all i am working on school project and i have a problem with

  • 0

greeting all
i am working on school project and i have a problem with my cod , what i am trying to do is returning two values.
any help appreciated 🙂

 #include <iostream>
 #include <string>
 using namespace std;

 // class for rational number 
class rational
  {
public:
// input & output the fraction 
void output(ostream& out);
void set(int num, int den);
int get_numerator();
int get_denominator();
// the operations
rational add(rational number);
rational sub(rational number);
rational mul(rational number);
rational div(rational number);
rational comp(rational number );

  private:
int numerator;
int denominator;
  };

  rational rational1, rational2; // global veriables to use at comparing operations 

rational rational::add(rational number) // addition operation 
 {
rational sum;
sum.numerator = numerator*number.denominator + denominator*number.numerator;
sum.denominator = denominator*number.denominator;
return sum ;
   }
 rational rational::sub(rational number) // subtract operation 
  {
rational sum2;
sum2.numerator =numerator*number.denominator - denominator*number.numerator;
sum2.denominator = denominator*number.denominator;
return sum2 ;
}
rational rational ::div(rational number) // divede operation 
{
rational sum3 ;
sum3.numerator =numerator*number.denominator ;
sum3.denominator = denominator*number.numerator;
return sum3 ;

}
 rational rational::mul(rational number) // multiply operation 
 {
rational sum4;
sum4.numerator =numerator*number.numerator;
sum4.denominator = denominator*number.denominator;
return sum4 ;
}
 rational rational::comp (rational number ) // compare operation 
 {
rational compare1 , compare2 ;
compare1.denominator = numerator*number.denominator ;
compare2.numerator = denominator*number.numerator;
if (compare1.denominator > compare2.numerator)
{ cout << endl ;
    cout << "the biggest fraction is  ";
    return rational1;

} else if (compare1.denominator < compare2.numerator ) 
{
    cout << endl ;
    cout << "the biggest fraction is  ";
    return rational2 ;
} else { 
    cout << " both fractions are equal ..." << rational2 << " = "; // here                                                                                                              
         is the error 
    return rational1  ;

}

   }
       void rational::set( int num, int den)
   {
numerator = num;
denominator = den;
}

   void rational::output(ostream& out = cout)
    {
out << numerator << "/" << denominator;
     }


    int main ()
   {
rational result ;
char salsh ;

cout << "please enter the numerator and denominator for the 1st fraction : ";
int num, den;
cin >> num >> salsh >> den;
if (den ==0)
{
    cout << "the denominator can't be zero " << endl ;
    system("pause");
    return 0;
}

else 
{
    rational1.set(num, den);

    cout << "please enter the numerator and denominator for the 2nd fraction :           
                    " ;
    cin >> num >>salsh >> den;
    if (den ==0)
    {
        cout << "the denominator can't be zero " << endl ;
        system("pause");
        return 0;

    }
    else {
        rational2.set(num, den);
        result = rational1.add(rational2);
        cout << "The result of adding them is: ";
        result.output(cout);
        cout << endl ;

        result = rational1.sub(rational2);
        cout << "The result of subtracting them is: ";
        result.output(cout);
        cout << endl ;

        result = rational1.div(rational2);
        cout << "The result of diveding them is: ";
        result.output(cout);
        cout << endl ;


        result = rational1.mul(rational2);
        cout << "The result multiplying them is: ";
        result.output(cout);
        cout << endl ; 

        result = rational1.comp(rational2) ;
        result.output(cout);
        cout << endl;
    }

}

system ("pause");
return 0;
     }

Error 1 error C2679: binary ‘<<‘ : no operator found which takes a right-hand operand of type ‘rational’ (or there is no acceptable conversion) c:\users\mike\documents\visual studio 2008\projects\mik\mik\michael shokery.cpp 73

here is the code

   inline ostream& operator<<(ostream& stream,  rational& rational)
{
 rational.output(stream);
 return stream;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:24:51+00:00Added an answer on May 28, 2026 at 1:24 pm

    The difference is that in one place you do

    cout << " both fractions are equal ..." << rational2 << " = "; // error here     
    

    while in the other places you do

        cout << "The result of adding them is: ";
        result.output(cout);   
    

    To have the << operator work for rational you need to define the operator for that type

    inline ostream& operator<<(ostream& stream, const rational& value)
    {
         value.output(stream);
         return stream;
    }   
    

    And to have the output function callable with the const parameter to operator<<, the function has to be constas well:

    class rational
    {
    public:
        // input & output the fraction
         void output(ostream& out) const;      // Add const here
    

    …

    void rational::output(ostream& out = cout) const   // and here!
    { 
        out << numerator << "/" << denominator;
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, all. I'm working on a training project; I have a page for user
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings all, I have a question. I am trying to build a parametrized query
Greeting, I have problem stopping FLVPlaypack component when I navigate from one frame to
Greeting all I update my field 'createdOn' datetime field to hold values of last
Greetings all Currently working on assessing a project, and wondered if there was a
Greeting, I have two levels of authentications in ASP.NET application. I have a Webadmin
Greetings all, I have a question. On my MOSS 2007 dev box, I created
Greetings all, I need help combining some htaccess rewrites, these crazy regular expressions screw
Greetings all, I'm trying to implement middleware (driver) for an embedded device with generic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.