LovesPie LovesPie - 11 months ago 46
C++ Question

C++ How do I print an object?

I have created a constructor

Location(double xIn,double yIn,string placeIn,int timeIn)
: x(xIn),y(yIn) on {

Say I want to print Location home(x,y,place,time); that's in the

How would I do so? I've been searching around and was told to use
. How would I implement this?

UPDATE: After creating some get methods and I tried doing,can't exactly compile it because of the problem

ostream &operator<<(ostream & o, const Location & rhs){

o << rhs.getX() << "," << rhs.getY() << "," << rhs.getPlace() << "," << rhs.getTime();
return o; }

Answer Source

Here is the stencil for overloading operator<<:

class Any
    friend std::ostream& operator<<(std::ostream& output, const Any& a);
    int member;

operator<<(std::ostream& output, const Any& a)
  output << a.member;
  return output;

This one possible stencil, there are other possibilities. Search the internet for "c++ stream insertion operator overload example" for other implementations.