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

  • Home
  • SEARCH
  • 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 6650617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:54:47+00:00 2026-05-26T00:54:47+00:00

Here is the prototype of my class Rational #ifndef RATIONAL_H #define RATIONAL_H //forward declaration

  • 0

Here is the prototype of my class Rational

#ifndef RATIONAL_H
#define RATIONAL_H

//forward declaration
class ostream;

class Rational
{
  int numerator,denominator;
  public:
  // the various constructors
  Rational();
  Rational(int);
  Rational(int,int);

  //member functions
  int get_numerator()const{return numerator;}
  int get_denominator()const{return denominator;}

  // overloaded operators
  // relational operators
  bool operator==(const Rational&)const;
  bool operator<(const Rational&)const;
  bool operator<=(const Rational&)const;
  bool operator>(const Rational&)const;
  bool operator>=(const Rational&)const;

  //arithmetic operators
  Rational operator+(const Rational&)const;
  Rational operator-(const Rational&)const;
  Rational operator*(const Rational&)const;
  Rational operator/(const Rational&)const;

  //output operator
  friend ostream& operator<<(ostream&, const Rational&);
};
#endif //RATIONAL_H

And this is the implementation of the overloaded output operator<< in rational.cpp

// friend output operator
ostream& operator<<(ostream& os, const Rational& r)
{
  os<<r.get_numerator()<<"/"<<r.get_denominator();
}

When I try to compile I get the following error

g++ -c rational.cpp
rational.cpp: In function ‘ostream& operator<<(ostream&, const Rational&)’:
rational.cpp:81:26: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
rational.cpp:7:1: error:   initializing argument 1 of ‘Rational::Rational(int)’ [-fpermissive]

I wanted to be able to display the rational number as numerator/denominator when it is passed to the << operator.

  • 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-26T00:54:48+00:00Added an answer on May 26, 2026 at 12:54 am

    Your first issue is that you try to forward declare ostream as a class. Assuming that you mean to use std::ostream, you can’t do that, its not legal.

    For one, it’s a typedef for a template specialization, not a class itself.

    Second, because you don’t #include <ostream> you don’t have a definition for any of the standard << overloads for ostream so when you try to << a string literal, the compiler trys to convert the string literal to a Rational type as that is the only type that has a << overload visible.

    Simply, you need to #include <ostream> and qualify ostream with std:: where you use it.

    A third point is that your overload of operator<< needs to return something. You should either append a return os; statement or simply return the whole streaming expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to translate Brandon Kelly's AC.VR class (Prototype) into a jQuery plugin. Here
I'm trying to translate Brandon Kelly's AC.VR class (Prototype) into a jQuery plugin. Here
template <class T> class Test { public: template<class T> void f(); //If i define
I have the function prototype here: extern C void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); I need to
So I decided to start using prototype and here's my first question. I'm trying
Here's a very simple Prototype example. All it does is, on window load, an
here is some sample javascript: SomeObjectType = function() { } SomeObjectType.prototype = { field1:
This is a snippet from a prototype class i am putting together. The scoping
Im using the Prototype Javascript Library's Window class to create pop up windows in
Here is the test code template <class T> void f() { T t; t.f<T>(0);

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.