overloading "<<" with a struct (no class) cout style

Posted by monkeyking on Stack Overflow See other posts from Stack Overflow or by monkeyking
Published on 2010-04-27T06:23:23Z Indexed on 2010/04/27 6:33 UTC
Read the original article Hit count: 215

Filed under:
|
|

I have a struct that I'd like to output using either 'std::cout' or some other output stream. Is this possible without using classes?

Thanks

#include <iostream>
#include <fstream>
template <typename T>
struct point{
  T x;
  T y;
};

template <typename T>
std::ostream& dump(std::ostream &o,point<T> p) const{
  o<<"x: " << p.x <<"\ty: " << p.y <<std::endl;
}


template<typename T>
std::ostream& operator << (std::ostream &o,const point<T> &a){
  return dump(o,a);
}


int main(){
  point<double> p;
  p.x=0.1;
  p.y=0.3;
  dump(std::cout,p);
  std::cout << p ;//how?
  return 0;
}

I tried different syntax' but I cant seem to make it work.

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl