overloaded stream insertion operator with a vector

Posted by Julz on Stack Overflow See other posts from Stack Overflow or by Julz
Published on 2010-04-02T01:40:20Z Indexed on 2010/04/03 0:23 UTC
Read the original article Hit count: 551

hi, i'm trying to write an overloaded stream insertion operator for a class who's only member is a vector. i dont really know what i'm doing. (lets make that clear) it's a vector of "Points" which is a struct containing two doubles. i figure what i want is to insert user input (a bunch of doubles) into a stream that i then send to a modifier method? i keep working off other stream insertion examples such as...

std::ostream& operator<< (std::ostream& o, Fred const& fred)
 {
   return o << fred.i_;
 }

but when i try a similar.....

 istream & operator >> (istream &inStream, Polygon &vertStr)
     {
        inStream >> ws;
        inStream >> vertStr.vertices;
        return inStream;
     }

i get an error "no match for operator >> etc etc. if i leave off the .vertices it compiles but i figure it's not right? (vertices is the name of my vector ) and even if it is right, i dont actually know what syntax to use in my driver to use it? also not %100 on what my modifier method needs to look like.

here's my Polygon class //header

#ifndef POLYGON_H
#define POLYGON_H
#include "Segment.h"
#include <vector>
class Polygon
{
 friend std::istream & operator >> (std::istream &inStream, Polygon &vertStr);
 public:
   //Constructor
    Polygon(const Point &theVerts);
    //Default Constructor
    Polygon();
    //Copy Constructor
    Polygon(const Polygon &polyCopy);
    //Accessor/Modifier methods
    inline std::vector<Point> getVector() const {return vertices;}
    //Return number of Vector elements
    inline int sizeOfVect() const {return (int) vertices.capacity();}
    //add Point elements to vector
    inline void setVertices(const Point &theVerts){vertices.push_back (theVerts);}

 private:
   std::vector<Point> vertices;
};
#endif

//Body

using namespace std;
 #include "Polygon.h"
// Constructor
Polygon::Polygon(const Point &theVerts)
    {
        vertices.push_back (theVerts);
        }
//Copy Constructor
Polygon::Polygon(const Polygon &polyCopy)
{
    vertices = polyCopy.vertices;
}
//Default Constructor
Polygon::Polygon(){}

istream & operator >> (istream &inStream, Polygon &vertStr)
 {
    inStream >> ws;
    inStream >> vertStr;
    return inStream;
 }

any help greatly appreciated, sorry to be so vague, a lecturer has just kind of given us a brief example of stream insertion then left us on our own

thanks. oh i realise there are probably many other problems that need fixing

© Stack Overflow or respective owner

Related posts about insertion

Related posts about vector