Solved

How to overload << operator for a class?

Posted on 2007-03-23
9
251 Views
Last Modified: 2010-04-01
Hi,

How do we overload the stream operator for a class again? I want to do this:

class Something {
    int a;
    double d;
    void OverloadedStreamOperator <<()
    {
        ????
    }
};

int main()
{
    Something s;
    s.a = 44;
    s.d = 34.34;
    cout << s << endl;
}

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18782467
 ostream& operator<< (ostream& os, const Something& s)
  {
    return os << s.a << " " << s.d << endl;
  }
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 18782538
I tried adding that as a member of my class but get this compiler error:

     binary 'operator <<' has too many parameters
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782537
Note that this is not a member operator, but an operator at global scope, so to make that work you'll usually have to make it a 'friend' of your class to use for non-public members also, i.e.

 ostream& operator<< (ostream& os, const Something& s)
  {
    return os << s.a << " " << s.d << endl;
  }
class Something {

friend ostream& operator<< (ostream&, const Something&);

    int a;
    double d;

};
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DJ_AM_Juicebox
ID: 18782552
is there any way to make it a member of the class? Otherwise I'll have all these friends for all my classes at global scope?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18782563
Or completely with your class :

class Something {
    int a;
    double d;
  public :
    friend istream& operator>>(istream& is, Something& s);
    friend ostream& operator<<(ostream& os, const Something& s);
};

istream& operator>>(istream& is, Something& s) {
  is >> s.a;
  is >> s.d;
  return is;
}
ostream& operator<<(ostream& os, const Something& s) {
  os << "a = " << s.a << endl;
  os << "d = " << s.d << endl;
  return os;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782586
>>is there any way to make it a member of the class?

No, the '<<' operator has a 'left to right' association. You can make a

Something s;

s << "whatever";

a member, but not a

ostream os;

os << s;
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18782660
BTW, you could also add a 'write()' method like

class Something {
    int a;
    double d;
    ostream& write(ostream& os) const
    {
        os << a;
        os << d;

        return os;
    }
};

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 18782690
yeah i ended up just doing a write() function. How sad!

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 18782711
I'd actually have chosen the 'friend' approach. It is just a C++ concept that your friends have access to your privates (pardon the pun ;o)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question