Search

Language Selector

9.3 — Overloading the I/O operators

Overloading <<

For classes that have multiple member variables, printing each of the individual variables on the screen can get tiresome fast. For example, consider the following class:

If you wanted to print an instance of this class to the screen, you’d have to do something like this:

And that’s just for one instance! It would be much easier if you could simply type:

and get the same result. By overloading the << operator, you can! Overloading operator<< is similar to overloading operator+ (they are both binary operators), except that the parameter types are different. Consider the expression cout << cPoint. If the operator is <<, what are the operands? The left operand is the cout object, and the right operand is your Point class object. cout is actually an object of type ostream. Therefore, our overloaded function will look like this:

Implementation of operator<< is fairly straightforward -- because C++ already knows how to output doubles using operator<<, and our members are all doubles, we can simply use operator<< to output the member variables of our Point. Here is the above Point class with the overloaded operator<<.

This is pretty straightforward -- note how similar our output line is to the line we wrote when we were outputting our members manually. They are almost identical, except cout has become parameter out! The only tricky part here is the return type. Why are we returning an object of type ostream? The answer is that we do this so we can "chain" output commands together, such as cout << cPoint << endl;

Consider what would happen if our operator<< returned void. When the compiler evaluates cout << cPoint << endl;, due to the precedence/associativity rules, it evaluates this expression as (cout << cPoint) << endl;. cout << cPoint calls our void-returning overloaded operator<< function, which returns void. Then the partially evaluated expression becomes: void << endl;, which makes no sense!

By returning the out parameter as the return type instead, (cout << cPoint) returns cout. Then our partially evaluated expression becomes: cout << endl;, which then gets evaluated itself!

Any time we want our overloaded binary operators to be chainable in such a manner, the left operand should be returned.

Just to prove it works, consider the following example, which uses the Point class with the overloaded operator<< we wrote above:

This produces the following result:

(2.0, 3.0, 4.0) (6.0, 7.0, 8.0)

Overloading >>

It is also possible to overload the input operator. This is done in a manner very analogous to overloading the output operator. The key thing you need to know is that cin is an object of type istream. Here’s our Point class with an overloaded operator>>:

Here’s a sample program using both the overloaded operator<< and operator>>:

Assuming the user enters 3.0 4.5 7.26 as input, the program produces the following result:

You entered: (3, 4.5, 7.26)

Conclusion

Overloading operator<< and operator>> make it extremely easy to output your class to screen and accept user input.

Before we finish this lesson, there is one additional point that is important to make. The overloaded output operator<<

is actually better written as

This way, you will be able to output both const and non-const objects. However, for the overloaded input operator>>, you will have to leave cPoint as non-const because the overloaded operator>> modifies cPoints members.

By now, you should be starting to become comfortable with classes and passing parameters by reference. In future lessons, we will start making more of our parameters const references (which we should have been doing all along, but have abstained for purposes of simplicity). It is a good habit to get into early.

9.4 -- Overloading the comparison operators
Index
9.2 -- Overloading the arithmetic operators

73 comments to 9.3 — Overloading the I/O operators

  • Jason

    I understand everything on this page well enough to overload I/O operators. I feel I grasp everything that has been presented here.

    Still, I’m left wondering what goes on under the hood. For example:

    What does the & (reference operator) do here: ostream& operator< < I believe this is the first time I've seen anything like that, a reference to a function or something? Does this maybe somehow tie into function pointers?

    I'm in a little over my head at this point. I'm wondering if it's even important to know what's going on under the hood with this issue.

    Your thoughts?

    • Jason

      What really had me confused was the return by reference statement. This ostream& operator< < was confusing me simply because returning a value by reference is something I had not seen or done up until this point. It took me a little while to make sense of this. Anyway, here is some code that I came up with that helped me understand what takes place when returning a value by reference.

      • There really isn’t much difference between return statements and function parameters, except for the directionality of the data transfer and the fact that you can only have one return value.

        Pretty much everything you’ve learned about pass by value, reference, and address apply to return values in the same way that they apply to parameters.

  • Jason, ostream is a class provided as part of C++ that handles outputs streams. The details of how ostream is implemented is very complex, but fortunately also completely unnecessary to use it effectively.

    Since ostream is a class, ostream& is a reference to an ostream class. Note that we’re also taking an ostream& as a parameter. ostream is typically passed by reference because we don’t want to make a copy of it as we pass it around.

    So basically, our overloaded function takes an ostream as a parameter, writes stuff to it, and then returns the same ostream. This allows us to chain < < calls together:


    cout < < cPoint1 << cPoint2 << cPoint3;

    This resolves as follows:


    ((cout < < cPoint1) << cPoint2) << cPoint3;

    cout < < cPoint1 is resolved first, with cout becoming the ostream& parameter. When this overloaded function is finished writing to the out parameter, it returns that cout so the next call to < < can use it. Thus:


    ((cout < < cPoint1) << cPoint2) << cPoint3;

    becomes:


    (cout < < cPoint2) << cPoint3;

    becomes:


    cout < < cPoint3;

    This calls our overloaded function one last time. At the end of this function, cout is again returned. But there's nobody left to use it, so the return value is ignored. The expression ends, and the program moves to the next line.

  • Jason

    Hmm, can I reference my own classes in a manner similar to the way ostream& references an ostream class? If that is possible, could it be useful.

    Curiosity killed the cat! :)

  • Yes. This is typically done in two ways.

    Let’s say you wrote a class named Foo.

    If you are writing a function that lives outside of Foo:

    If writing a function that is a member of Foo:

  • Jason

    Then I shall in good time have to experiment coding a reference to a class. I imagine this will increase my understanding of the ostream implementation.

  • Tom

    Alex -

    I don’t understand why this:

    isn’t written like this:

    I guess I don’t get the difference between “out” and “cout”.

    ????

    Thanks.

    • Good question! Here’s what I think you might be missing: out might not be cout. It might be cerr (an output stream used for error conditions), or a file ostream object (an output stream for writing to a file instead of the screen). You’ll learn about both of these in the chapter on I/O. In those cases, we don’t want to write to cout, we want to write to whatever alternative output stream we’re using. Those alternative streams come into the overloaded < < operator as the "out" parameter. For example:
      cerr < < "You have hit error # " << nErrorNum << endl;

      This is broken up like this:


      ((cerr < < "You have hit error # ") << nErrorNum) << endl;

      cerr's overloaded extraction operator will receive cerr as the "out" parameter, and "You have hit error # " as a string parameter. When it is done loading the stream, it will return cerr. Then evaluation will continue, and we will have:


      (cerr < < nErrorNum) << endl;

      cerr's overloaded extraction operator will receive cerr as the "out" parameter, and nErrorNum as an integer parameter. When it is done putting the value of nErrorNum in the stream, it will return cerr again, and evaluation will continue:


      cerr < < endl;

      Again, cerr's overloaded extraction operator will receive cerr as the "out" parameter, and endl as it's other parameter. It will do it's thing, return cerr, and evaluation will continue. At this point, there's nothing left to evaluate, so evaluation is complete.

      If we had returned cout instead of out, then nErrorNum and endl would have been printed to cout instead of cerr!

  • simon simpson

    I am using Visual Studio 2005.
    I can’t get the following to compile:

    friend ostream& operator<< (ostream &out, Point &cPoint);

    I get two errors relating to this:

    error C2143: syntax error : missing ‘;’ before ‘&’

    error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

    I have used the Point example listed above exactlly.

    Can anyone help?

    • davidv

      error C2143: syntax error : missing ‘;’ before ‘&’

      to fix this, simply put semi-colon at the end of the previous function declaration

      error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations

      this will be fine if you put “using namespace std” right at the beginning of your code, so that it has global impact

      • mezentius

        I had both errors (along with an error for practically every other character on the line), and fixed all of them with just “using namespace std”. Weird.

  • siku

    public:
    Point(double dX=0.0, double dY=0.0, double dZ=0.0)
    {
    m_dX = dX;
    m_dY = dY;
    m_dZ = dZ;
    }; //Put semicolon on this line, it should help!

    friend ostream& operator<< (ostream &out, Point &cPoint);

    ” PRE” tags doen’t work for me.

  • M.N. Jayaram

    Hi

    The operator << being a friend of the class can directly access the member variables & avoid the costly function calls as below.

    [ Good point. Examples updated. Thanks! -Alex ]

  • George D

    Hi

    I also have a problem that I cannot understand. I tried to compile my code with GNU C++. Here is a part of my code:

    The header file

    The source:

    The compiler sends the following error message: “/home/demarcsek/dev/Kalaha3/src/KCompress.h:63: error: ‘std::ostream& Kalaha::KCompress::BitArray::operator<<(std::ostream&, const Kalaha::KCompress::BitArray&)’ must take exactly one argument”
    When I modified this function with only one argument as a const BitArray& and used cout as a default stream, the compiler wrote the following message: “/home/demarcsek/dev/Kalaha3/src/KCompress.h:63: error: ‘std::ostream& Kalaha::KCompress::BitArray::operator<<(std::ostream&, const Kalaha::KCompress::BitArray&)’ must take exactly two arguments”

    It’s funny…Can anybody help me?

    Thanks,
    George

    • Joris

      ostream& BitArray::operator<< ( ostream& _stream, const BitArray& _arr )
      { //implementation
      return _stream;
      }
      That’s wrong - it shouldn’t be member function of the Bitarray class. Instead, it should be a “global” function:

      Instead use this:

      ostream& operator<< ( ostream& _stream, const BitArray& _arr )
      { //implementation
      return _stream;
      }

  • rehab

    why it should be friend?

    • Benjamin

      If you implement it as a member, it will have to be a member of ‘ostream’, taking one ‘Point’ as parameter:

      In c++ we can’t reopen and extend the ostream-class, so we have to use a friend function.

      Think of it the other way:
      if you implemented it as a member of ‘Point’…

      … you could pass a ‘ostream’ to the ‘Point’ class through the ‘<<‘-operator:

      This compiles, but (in our case) it doesn’t make any sence.

      Hope this helped!

      [offtopic]in some languages (i.e. ruby) you can reopen and extend existing classes (i.e. ‘ostream’)[/offtopic]

  • RevWaldo

    In the above example - any many others like it on the web - the code is written thus:

    Meanwhile many other texts - including a C++ textbook I’m using - would write it as:

    I imagine the complier just ignores the difference in whitespace, but what’s up with that? The first version makes more sense to me, while the second makes me scratch my head. Can anyone clarify why they’d write it differently? Or are they actually different?

    • serenity

      This is purely a matter of style. Choose whichever you prefer, since as you guessed, the whitespace is ignored by the compiler.

      I don’t have a reason for it that I can explain, but I prefer “char *func()” and “char &func()” over “char* func()” and “char& func()”. I guess I was raised that way. 😉

  • bob

    Ok, so how would you overload the input operator if you wanted to input both an integer and a string (such that the string could pick up long srings with spaces, not single words…)…

    For example:

    The above will only take in a word. Apologies if this is a basic question. I’m a first timer.

    - Bob

  • the line cout<<date1+date2 outputs wrong result, i cannot find why..

  • RSA

    this should work now

    • Yes, it works, thanks for your solution. Furthermore, can you explain me why, thanks in advance:)

      • RSA

        This type of error is compiler specific. The reference of the newly created date is no longer valide when the function exits because all the data inside that function are all destroyed. There are few ways to solve this problem, for example you can make static your variable if you consider that its lifetime to be the whole execution of the program. You can make it global and assign with the new operator like I did, but you musn’t forget to delete all variables that used “new” (when the program ends the os deletes them automatically). Here it worked because pointer created with “new” remained in the memory. But you should make it global and delete it if you want to avoid memory leak during execution, or make it static.

        • Rahul

          Hi RSA,

          As per your explanation, how come default copy constructor manages to perform the similar kind of access which overloaded<< operator could not able to perform.

          • Alex

            The problem with the original program is that it was returning a local (anonymous) variable by reference. That means it was returning a reference to something going out of scope.

            RSA’s solution actually isn’t the best, because a developer wouldn’t expect operator+ to dynamically allocate a new instance of Date. A better thing to do would be this:

            That way, a copy of the date is returned by value.

  • Sander

    I’m getting those errors trying to compile the last one with giving the point.
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2143: syntax error : missing ‘;’ before ‘&’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2433: ‘ostream’ : ‘friend’ not permitted on data declarations
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2061: syntax error : identifier ‘ostream’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : error C2805: binary ‘operator <d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2143: syntax error : missing ‘;’ before ‘&’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2433: ‘istream’ : ‘friend’ not permitted on data declarations
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2061: syntax error : identifier ‘istream’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(21) : error C2805: binary ‘operator >>’ has too few parameters
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2143: syntax error : missing ‘;’ before ‘&’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2086: ‘int ostream’ : redefinition
    1> d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(20) : see declaration of ‘ostream’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: ‘out’ : undeclared identifier
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2065: ‘cPoint’ : undeclared identifier
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C2275: ‘Point’ : illegal use of this type as an expression
    1> d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(8) : see declaration of ‘Point’
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\documents\visual studio 2008\projects\tryh9\tryh9\tryh9.cpp(28) : fatal error C1903: unable to recover from previous error(s); stopping compilation

    how is it possible?

  • awa

    #include
    using namespace std;

    int x = 11;

    int& ref()
    {
    return x;
    }

    int main()
    {
    int *ptr = &ref(); //after the return by reference
    //takes place, &ref() is &x

    *ptr = 12;
    cout < < x << endl;

    return 0;
    }

  • saini

    hi
    would anyone help me to understand that why are we passing both the arguments as a reference and what will happen if we pass them as a value parameter

  • saini

    • Gammerz

      Alex replied to this above:-

      Since ostream is a class, ostream& is a reference to an ostream class. Note that we’re also taking an ostream& as a parameter. ostream is typically passed by reference because we don’t want to make a copy of it as we pass it around.

  • Gammerz

    When the operator<< function becomes a friend of class Point, it allows us direct access to the member variables m_dX, m_dY and m_dZ. We could avoid making the operator<< a friend of the class Point and instead use member functions GetX(), GetY() and GetZ(), for improved encapsulation. Is this example purely to demonstrate the use of the "friend" command or are we saying this is a preferred method?

  • ARVIND

    While overloading output stream operator << as member member function I am getting following error:

    ostreamOprt.cpp:32: error: no match for 'operator<<' in 'std::cout << objs'
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63: note: candidates are: std::basic_ostream& std::basic_ostream
    ::operator<<(std::basic_ostream&(*)(std::basic_ostream&)) [with _CharT = char, _Traits = std:
    :char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74: note: std::basic_ostream& std::basic_ostream::operator<<(std::basic_ios&(*)(std::basic_ios&)) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:86: note: std::basic_ostream& std::basic_ostream::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:121: note: std::basic_ostream& std::basic_ostream::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:155: note: std::basic_ostream& std::basic_ostream::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:98: note: std::basic_ostream& std::basic_ostream::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:178: note: std::basic_ostream& std::basic_ostream::oper
    ator<<(short int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:189: note: std::basic_ostream& std::basic_ostream::oper
    ator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:193: note: std::basic_ostream& std::basic_ostream::oper
    ator<<(int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:204: note: std::basic_ostream& std::basic_ostream::oper
    ator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:179: note: std::basic_ostream& std::basic_ostream::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:214: note: std::basic_ostream& std::basic_ostream::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:238: note: std::basic_ostream& std::basic_ostream::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:219: note: std::basic_ostream& std::basic_ostream::oper
    ator<<(float) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:261: note: std::basic_ostream& std::basic_ostream::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:284: note: std::basic_ostream& std::basic_ostream::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:307: note: std::basic_ostream& std::basic_ostream::operator<<(std::basic_streambuf*) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:449: note: std::basic_ostream& std::operator<<(std::basic_ostream&, char) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:505: note: std::basic_ostream& std::operator<<(std::basic_ostream
    &, char) [with _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:460: note: std::basic_ostream& std::operator<<(std::basic_ostream&, signed char) [with _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:465: note: std::basic_ostream& std::operator<<(std::basic_ostream&, unsigned char) [with _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:567: note: std::basic_ostream& std::operator<<(std::basic_ostre
    am&, const char*) [with _CharT = char, _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:612: note: std::basic_ostream& std::operator<<(std::basic_ostream
    &, const char*) [with _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:499: note: std::basic_ostream& std::operator<<(std::basic_ostream&, const signed char*) [with _Traits = std::char_traits]
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:504: note: std::basic_ostream& std::operator<<(std::basic_ostream&, const unsigned char*) [with _Traits = std::char_traits]

    Alex can you please explain why?

  • rebelsoul

    Hello Alex,
    Thank you for such a good tutorial. I am facing a problem while running the above given code. I am using visual studio 2008, following your code and same input. However in result only the first input variable value is printed the rest are discarded.
    Here is a extract after debug.
    cPoint {m_dX=6.0000000000000000 m_dY=0.00000000000000000 m_dZ=0.00000000000000000

    Could you please guide, why the program is not taking all the value?

  • sw1983

    Hello, my compiler (visual c++ 2010) will not recognise ostream. It is saying it is an undeclared identifier. I’ve double checked and ostream is in the iostream library, I’ve included iostream. I thought it was something I was doing but I have copied and pasted Alex’s code into my program and still I get the same error. What am I missing? I haven’t written the function yet because the syntax gets underlined in red so I know it won’t compile anyway.


    #include <iostream>
    #include <string>

    class Employee
    {
    private:
    static int sm_nID;
    int m_nID ;
    char m_strName[25];
    double m_dWage;

    public:
    Employee(char *strName="Blank", double dWage=10.00)
    {
    using namespace std;
    strncpy_s(m_strName, strName, 25);
    m_nID = ++sm_nID;
    m_dWage = dWage;
    }

    char* GetName()
    {
    return m_strName;
    }

    int GetID()
    {
    return m_nID;
    }

    double GetWage()
    {
    return m_dWage;
    }

    void Print()
    {
    using namespace std;
    cout << "Name: " << m_strName << "\t" << "Employee ID: " << m_nID << "\t" <<
    "Wage: " << m_dWage << "\n";
    }

    static void PrintID()
    {
    std::cout << sm_nID;
    }

    friend void ResetName(Employee &cEmployee);
    friend class FriendOfMine;
    /****************************HERE**************************************************/
    friend ostream& operator<< (ostream &out, Employee &cEmployee);
    };

    /****************************HERE**************************************************/

    ostream& operator<< (ostream &out, Employee &cEmployee)
    {

    }

    int Employee::sm_nID = 0;

    void ResetName(Employee &cEmployee)
    {
    strncpy(cEmployee.m_strName, "Reset", 25);
    }

    class FriendOfMine
    {
    public:
    void WageChange(Employee &cEmployee)
    {
    cEmployee.m_dWage = 5;
    }
    };

    int main()
    {

    return 0;
    }

  • sw1983

    Nevermind I’d forgotten to use the namespace. haha, kick myself.

  • zynk

    ostream& operator<< (ostream &out, Point &cPoint)
    {
    // Since operator<< is a friend of the Point class, we can access
    // Point's members directly.
    out << "(" << cPoint.m_dX << ", " <<
    cPoint.m_dY << ", " <<
    cPoint.m_dZ << ")";
    return out;
    }

    Hey Alex
    we are returning a reference to the object 'out', which has local scope. Is it legal??

  • etam

    You should mention that “<>” are originally arithmetic bitwise shift operators. http://en.cppreference.com/w/cpp/language/operator_arithmetic

  • Monster

    Thanks alot for accepting me as a member in this great website.
    I am interested in learning C++ so much and this website is a great source to depend on in learning C++…..

  • yakisobskie

    Hi Alex! Great tutorial! btw Everything worked except for the operand>> tutorial. I’m getting an error “no match for operator>>…”. I’m unable to understancd istream.

    How come there is no match for operand>>(istream, double) while
    operand<<(ostream, double) is working fine? Thanks!

    01| #include
    02| using namespace std;
    .
    .
    .

    34| istream& operator>> (istream& x, const Point& y)
    35| {
    36| x >> y.m_dX;
    37| x >> y.m_dY;
    38| x >> y.m_dZ;
    39| return x;
    40| }

    |36|error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream}’ and ‘const double’)|

  • Aston

    In checking the final example of the “Overloading <<" section, I ran into a peculiar output that I can't understand. I've had a look through the comments, but can't see anyone else mentioning it!

    cPoint1 and cPoint2 are fed doubles, and the constructor takes those doubles and stores them in private variables of type double. These are then fed through the overloaded << operator, and *should* be printed in their native double form, to my understanding… however when I run the complete program I get the following as output:

    (2, 3, 4) (6, 7, 8)

    I can't see any point in the code where the doubles should be collapsed to integers, can anyone help? I literally copy-pasted the last two codeblock examples from the section to play around with, and added the required include for iostream and the std namespace at the top.

  • nice meme

    friend ostream& operator<< (ostream &out, const Point &cPoint);

    what does ostream& mean?

    • Tony

      << is a binary operator that links a stream and whatever is output to it. Also, << chains. So when you overload << you want your function to return a reference to a stream. Hence ostream& is the type of your overloaded << function.

  • Tony

    Thanks for this great, great website.

    I am trying to put 9.2 and 9.3 together: overload +, and overload <<. Each of them works by itself. But if I put them together

    my compiler (Mac OS X 10.8 clang++) complains

    <b>operators 9.3 b.cpp:137:7: error: invalid operands to binary expression
          ('ostream' (aka 'basic_ostream<char>') and 'Point')
            cout << cPoint1 + cPoint2;
            ~~~~ ^  ~~~~~~~~~~~~~~~~~
    </b>
    followed by many many errors against /usr/include/c++/4.2.1/ostream like this one:
    <b>
    /usr/include/c++/4.2.1/ostream:112:7: note: candidate function not viable: no
          known conversion from 'Point' to '__ostream_type &(*)(__ostream_type &)'
          for 1st argument
          operator<<(__ostream_type& (*__pf)(__ostream_type&))
    </b>

    Here's the code:

    • Tony

      (I know RSA answered this question above, but I'm not really getting the answer. The code I posted (without the line that doesn't compile) works fine (and follows the code in 9.1 for MinMax) so returning a Point seems to yield a value in the calling program that works fine for assignment to p2. Why doesn't it parse as of the right type for the overloaded <<?)

  • Sabrina

    I have an ostream operator overloader, and I am copying in the rhs by reference. However, the information that should be passed in is gone, and the assert inside it fires. It is a copy constructor, passing in a class. The code is as follows:

    So, my question is this: is the operator overloaded at fault? I checked, and before it goes into the << function, the Data class still has the right information. When the assert fires, it says that month = 0, when I inputted 12, or some other number. I double checked that my copy constructor was working, so I’m not really sure what’s next. This is my first time working with operator overloaders, so any help is definitely welcome. I hope I included enough information!

  • Muhammad Wahaj Khan

    I am fall in love with this site….so helpful…solve my problem regarding the concepts of overloading…keep it up this good work…again thanks. 😀

  • john

    In the function declaration:

    friend ostream& operator<< (ostream &out, Point &cPoint);

    the out object is passed in by reference. But where in the code
    does the out object come from.

    thanx.

  • djcowboy

    Hi Alex
    Here’s my code. I’m compiling it in code blocks.

    The output is (2, 3, 4) (5, 6, 7)
    Why am I getting integer values?

  • above program crashes after taking the character input(char*) in function operator >>, Why and how to get rid of it?

    • Alex

      A few things here:
      1) Your overloaded operator >> and << both need to return ope.
      2) I put your program in a debugger and it’s crashing on the line where it’s reading obj.m_char with the following error message: Unhandled exception at 0x01122B8B in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x0112EB64. Consider what’s happening here: When var is created, you’re pointing m_char at a string literal. In the lesson on string literals, I mentioned that string literals can and often live in read-only memory. When your input function tries to input m_char, you’re trying to overwrite a string literal in read-only memory, so you’re getting a memory access violation.

      It’s a little weird to me that you’re declaring a variable with default values and then asking the user to overwrite them. Generally you’d want to do one or the other.

      That aside, a fix is easy: change m_char from type char* to type std::string. That way, std::string can dynamically manage the memory needed to hold the string for you. You’ll also need to update your ret_char() function:

  • Sachin

    Alex, you are an excellent teacher, thank for providing this useful site, thank you  very much  :)

  • Quang

    Dear Alex!! I’m trying my best to learn C++, so please help me out:
    - Why do you use ostream instead of Point as the friend function’s type although the function itself take objects from both ostream and Point class? Is ostream somehow a priority?
    - Why do we need to make reference like this:  friend ostream& operator….
    Thank you so much !

    • Alex

      Having the operator return ostream allows us to “chain” multiple inputs together:

      If we didn’t return an ostream, then we’d have to print each of these separately, like this:

      We make ostream a reference to prevent C++ from making a copy of the ostream argument every time the operator is called, or when ostream is returned.

  • Kyle

    Alex,

    For the Point class I overloaded opeartor+ and operator- to add and subtract x, y, and z for each point.

    I have the overloaded operator<< in my program and it works correctly.  I tried to add two points in a cout call and got errors at compile saying invalid operands to binary expression for <<.  However if I add the points into a new point and then try to print it there is no issue.  I can’t see why I can’t do it as an anonymous variable since my overloaded + for point returns a Point and << knows how to deal with that.

    A little more of the error
    invalid operands to binary expression (‘ostream’ (aka ‘basic_ostream<char>’) and ‘Point’)

    • Alex

      I was able to compile and run this program successfully in Visual Studio 2015. Can you provide the entire error message that you are getting, including which line it is complaining about?

      • Kyle

        I’m on a Mac, but haven’t had any different behavior with the clang compiler before.
        Full code:

        First two errors:
        repro.cpp:62:12: error: invalid operands to binary expression (‘ostream’ (aka ‘basic_ostream<char>’) and ‘Point’)
                std::cout << point1 + point2 << ‘\n’;
                ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ostream:179:20: note:
              candidate function not viable: no known conversion from ‘Point’ to ‘std::__1::basic_ostream<char>
              &(*)(std::__1::basic_ostream<char> &)’ for 1st argument
            basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&));
                           ^

        After that are a lot more notes about not being able to convert from Point to different types for the 1st argument.  It seems to think that the first argument to << is a Point (I’m guessing point2).  But that doesn’t make much sense because the operator precedence should complete point1 + point2 and return a Point, then << should happen left to right and my overloaded operator<< would then get the arguments with the ostream as the first argument.

        • Alex

          I think I see the problem here.

          If you look at your overloaded output operator:

          Note that Point is a non-const reference parameter.

          But when you call it:

          point1 + point2 is an rvalue and doesn’t have an address, so it can’t be passed to a non-const reference.

          The solution is easy: make your Point parameter const. That way it can take either an lvalue or an rvalue argument. You should be doing this anyway since your output operator doesn’t modify the Point parameter.

          • Kyle

            Thanks, that was it.  Now I see it, but the compiler was not very helpful here.  I can usually puzzle out the errors but wasn’t able to this time.  Thanks for the help.

  • Rahul

    Here, why I am not allowed to make any of these as constants.

  • Lamoer

    Alex, I have trouble when puttint you code in my machine:

    And these are the error:

    How should I fix it?

    Thanks!

  • Annibale

    I have tried with this code (on C++11) and I get a bunch of errors when I compile. I can not figure out why it does not work. thanks in advance for any help!

  • Reaversword

    Ok, I have a question about.

    Playing with this, I’ve realized its possible the operator the user chooses to overload can "get confused to ostream".

    So I’ll be short this time. Two 3d points.

    point1(4,5,6)
    point2(1,2,3)

    1)We have ready an overload for ostream that show us the 3d point we send.
    2)We overload a sum of the points with "operator+".

    Its perfectly possible write:

    cout<<point1+point2<<endl;

    But if we change operator+ for, (for example), "|" or "^" in step 2, then it isn’t possible get this working:

    cout<<point1^point2<<endl;

    We then need to put it in this way:

    cout<<(point1^point2)<<endl;

    I guess is a matter of precendences, or maybe a special way ostream haves to interpret that operators.

    I’ve no idea why happens this, but I suspect the mystery will be revealed in I/O lesson (chapter 13).

    Anyway, if this points deserves a menction, I’m looking forward to know a little bit more now.

    • Alex

      It’s a matter of precedence.
      + has higher precedence than <<, so cout << point1 + point2 << endl; gets prioritized as cout << (point1 + point2) << endl; ^ has lower precedence than <<, so cout << point1 ^ point2 << endl; gets prioritized as (cout << point1) ^ (point2 << endl); That's why the explicit parenthesis are necessary in the second case.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter

  

  

  

one × four =