Thread: Simple Overloading of Operator '<<'

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question Simple Overloading of Operator '<<'

    Yes.. yes.. i know this topic has been covered many times..
    I've looked through other posts but cannot find the solution i am looking for.. here is my problem:

    Code:
    int main()
      {
        Rational result;
        cout << result;
       return 0;
      }
    Code:
    class Rational
      {
        public:
          Rational();
          friend ostream &operator<<(ostream &out, Rational &c);
        private:
          int numerator, denominator;
      };
    
    Rational::Rational()
      {
        numerator = 0;
        denominator = 1;
      }
    
    ostream &operator<<(ostream &out, Rational &c)
      {
        out << c.numerator << "/" << c.denominator;
       return out;
      }
    someone want to tell me what i'm doin' wrong?
    shouldn't 0/1 be outputed to the screen?
    i understand overloading operators but this is the first time i've ever tried to overload the '<<' operator.
    compiler is giving me following errors:

    friends must be functions or classes.
    declaration missing ;
    declaration does not specify a tag or identifier.
    Declaration syntax error.
    Undefined symbol cout.

    This is not my entire code.. but i believe i've supplied enough info..
    so any ideas what i am doing wrong?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Do you at some point #include <iostream> and bring std::cout into the global namespace with a using directive?

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ostream &operator<<(ostream &out, Rational &c)
    {
    out << c.numerator << "/" << c.denominator;
    return out;
    }


    mebe that "out" is supposed to be a "cout" ?

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Nope...

    That part is correct.

    out is one of the parameters passed to the operator<<, allowing it to direct output to any ostream or subclass thereof. Hard coding cut as the ostream would decrease flexibility and shouldn't have any effect on the problem, if the operator is written correctly...

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    My mistake, thanks for correcting me.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I dont know whats wrong with your code or what compiler your using but this works fine for me. all i did was join your code which you prlby have and it print 0/1 no problem.

    Code:
    #include <iostream>
    #include <conio.h> // for my compiler
    using namespace std;
    
    class Rational
      {
        public:
          Rational();
          friend ostream &operator<<(ostream &out, Rational &c);
        private:
          int numerator, denominator;
      };
    
    Rational::Rational()
      {
        numerator = 0;
        denominator = 1;
      }
    
    ostream &operator<<(ostream &out, Rational &c)
      {
        out << c.numerator << "/" << c.denominator;
       return out;
      }
    
    
    
    int main()
      {
        Rational result;
        cout << result;
        getch();  // for my compiler
       return 0;
      }
    TRY IT maybe you made them serperate filse or something.

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Oh, yeah...

    it just occured to me that you have to bring std:stream into scope as well, or be explicit about it. Or just bring the whole std namespace in scope. But that is probably your problem, otherwise it should work fine, it just needs you to tell is what an ostream is, by bringing the relevant declaractions in scope.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    oh.. perhaps it is because i have separate files.. yeah i had
    #include <iostream>
    using namespace std;

    but it is not in the same file as the Rational class.. perhaps this is my problem... well atleast the code i wrote was right.. thats a bit encouraging ... i'll try and re-arrange my #include.. have a feeling it will fix the problem.. thnx for the help...

  9. #9
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Remember, you can #include <iostream> as many times as you want to in your project, it is advisable to include it at the top of the header for any class that requires it (the type ostream is part of the <iostream> header).

    So at the top of the header file for Rational, and at the top of the main .cpp file, make sure to #include <iostream> and bring at least std::ostream and std::cout into scope.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    btw.. i tried it and it works fine now..

    u can include more than once..? is this advisable?
    i thought i tried it awhile back and it gave me an error like multiple includes or includes nested too deep or something strange like that... hmm.. ill have to give'er a try .. it would make things alot easier..

  11. #11
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    The standard headers have constructs called "include guards" (Which you can and should add to your headers as well) which prevent them from having problems by being included more than once.

    For this reason, it is best to include every header that a file might need at the top of it, because it can't cause any harm and can prevent you from dealing with situations involving files needing to be included in a certain order.

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    The standard headers have constructs called "include guards" (Which you can and should add to your headers as well) which prevent them from having problems by being included more than once.

    For this reason, it is best to include every header that a file might need at the top of it, because it can't cause any harm and can prevent you from dealing with situations involving files needing to be included in a certain order.
    Correct me if I'm wrong.
    This is why we use
    #ifndef HEADER_H
    #define HEADER_H
    ....
    ....
    #endif

    Where HEADER_H is the name of the header file.

  13. #13
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Right you are. That construct is called an "include guard"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM