943,962 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 468
  • C++ RSS
Aug 2nd, 2009
0

operator<< overloading for a template class

Expand Post »
Folks,

I'm having some trouble with overloading the '<<' operator for a templated class. For example, given a very simple template class like this:

C++ Syntax (Toggle Plain Text)
  1. template<class T = double> class Real
  2. {
  3. T m_value;
  4. public:
  5. Real( T val = 0 ){
  6. m_value = val;
  7. }
  8. T get() const {
  9. return m_value;
  10. }
  11. };

This class rests inside a namespace. I've looked at the code in the <complex> header but I really want a simpler block than that. The template parameter could be int, float, etc.

Halp!

Thanks...
Sean
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Aug 2nd, 2009
0

Re: operator<< overloading for a template class

which part of overloading the << operator are you having
trouble with?

your prototype could be something like this :
C++ Syntax (Toggle Plain Text)
  1. std::ostream& operator<<(std::ostream& os, Real<T>& real);
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: classes
Next Thread in C++ Forum Timeline: life simulation project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC