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 7894441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:23:55+00:00 2026-06-03T07:23:55+00:00

so, I have been having issues in this header file overloading the stream insertion

  • 0

so, I have been having issues in this header file overloading the stream insertion operator. I receive the error message in the title if I use the code as is. But when I put the declaration in the main file, it works fine.

Rational.h

#ifndef RATIONAL_H
#define RATIONAL_H

using namespace std;
class Rational{
private:
    int numerator;
    unsigned int denominator;
    bool isNegative;

public:
    Rational();
    Rational(int);
    Rational(int, int);

    bool operator==(const Rational&);
    Rational& operator++(int);          //Unused int
    Rational operator-(const Rational&);
    Rational operator+(const Rational&);
    Rational operator*(const Rational&);
    Rational operator/(const Rational&);
};

ostream& operator<<(ostream&, Rational&); //Erroneous code


#endif

The other two files, 1.c and Rational.c if needed:

#include "Rational.h"
#include <iostream>
#include <math.h>

using namespace std;

Rational::Rational(){
numerator   = 0;
denominator = 1;
}

Rational::Rational(int num){
numerator   = num;
denominator = 1;     
}

Rational::Rational(int num, int den){
//Determine negativity
if(num < 0 xor den < 0){    //If negative
    if(num > 0){
        num *= -1;
    }
}

numerator   = num;
denominator = abs(den);  
}

bool Rational::operator==(const Rational& rhs){
return (numerator/(double)denominator == rhs.numerator/(double)(rhs.denominator));
}

ostream& operator<<(ostream& os, Rational& input){
os << "Moo";
return os;
}
/*
private:
    int  numerator;
    unsigned int  denominator;
    bool isNegative;

public:
    Rational(int, int);

    bool operator==(const Rational&);
    Rational& operator++(int);          //Unused int
    Rational operator-(const Rational&);
    Rational operator+(const Rational&);
    Rational operator*(const Rational&);
    Rational operator/(const Rational&);
*/

1.c

#include <iostream>
#include "Rational.h"

using namespace std;

int main(){
Rational test = Rational(2);

cout << test << endl;
}
  • 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-06-03T07:23:56+00:00Added an answer on June 3, 2026 at 7:23 am

    You need to include iostream in your Rational.h

    //Rational.h

    #include <iostream>
    

    When you put the overload declaration in 1.c, iostream is included before Rational.h and hence compiler knows the type ostream and there is no error.

    However, Rational.h does not include iostream so in that case compiler does not know the type ostream and hence the error.

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

Sidebar

Related Questions

I have been having this issue in iterating through an array of keys and
As many of you may already know I have been having issues with another
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
I have been having an issue with the $this->delete() method that deletes a record
We have been having some debate this week at my company as to how
I have been having issues with a custom check box control for a while
So I have recently migrated to postgresql, and have been having issues with my
I have been working all night and yesterday on this header section in css,
been having this problem for a while! anyway i have downloaded Maxminds GEOIP php
I've been having issues with this for a little while now. I feel like

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.