0

我正在尝试为唯一成员是向量的类编写重载流插入运算符。它是一个s的向量Point,它是一个struct包含两个doubles的a。我想我想要的是将用户输入(一堆doubles)插入到流中,然后发送到修饰符方法。我正在处理其他流插入示例,例如:

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

但是当我尝试类似的事情时:

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

我收到一个错误“不匹配operator >>等”。如果我不使用.vertices,它会编译,但我认为这是不对的。(顺便说一句,vertices是 my 的名称vector <Point>。)即使它是正确的,我实际上也不知道在我的程序中使用什么语法来使用它,而且我也不能 100% 确定我的修饰符方法需要看起来像。

这是我的Polygon课:

//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;
 }

很抱歉这么含糊;一位讲师刚刚给了我们一个流插入的简短示例,然后就让我们自己离开了。

4

1 回答 1

1

问题是向量没有标准的提取运算符(插入到流中,从流中提取),因此您需要定义自己的。此外,流默认情况下会跳过空格,因此您通常不需要使用std::ws. 您没有定义向量输入的终止方式,所以我假设换行符表示向量的结束。

由于这是学校的问题,我不能给你太多。首先,这里有一些声明供您填写,其中一些可能有用。导入 namespace 是不好的形式std,所以下面不会。

#include <list>

// returns true if ch is a horizontal space. Locales are a little tricky,
// so you could skip them for now and instead start with the functions defined 
// in header <ctype>
bool ishs(char ch, std::locale loc=std::locale::global());
// return true if ch is a vertical space
bool isvs(char ch);
// return true if the next character in stream 'in' is a vertical space.
bool eol(std::istream& in);

// reads & discards horizontal spaces
std::istream& hs(std::istream& in);

class Point {
public:
  // Scalar is so you can use 'Point::Scalar' rather than 'double', 
  // making it easy should you which to change the type that a Point holds.
  // When you've covered templates, this will make it trivial to templatize Point.
  typedef double Scalar;
  ...
};

class Polygon {
public:
     // adds pt as the last of this polygon's vertices
     // Note: this is basically your "setVertices" with a different name
   Polygon& append(const Point& pt);
     // adds the points from 'start' to 'end' to this polygon
   template <typename _Iter>
   Polygon& append(_Iter start, _Iter end);
     // remove all points in this polygon
   void erase();
     // returns the number of sides on this polygon, 
     // which is also the number of vertices
     // Note: this is different from your "sizeOfVect"; see below for more
   int sides() const { return vertices.size(); }
     // set aside space for polygon to have 's' sides.
   voids sides(int s) { vertices.resize(s); }
}

/* reads the next two numbers on the current line into pt.
   Throws an exception if there is only one number.
 */
std::istream& operator>>(std::istream& in, Point& pt);
/* reads numbers on the current line into points on 'poly'.
   Throws an exception if there is only one number. Preferably,
   won't alter 'poly' if there are an odd amount of numbers.

   you could also put the contents of this operator into >>(istream&, Polygon&)
 */
std::istream& operator>>(std::istream& in, std::vector<Point>& vertices) {
    std::list<Point::Scalar> points;
    Point pt;
    // while not at eol(in), read into pt and add it to points
    // After that, empty vertices, then add the Points in points to vertices
    ...
}

作为字符相关函数 ( ishs, isvs, hs, eol) 的替代方法,您可以将下一行读入字符串getline,然后读入 anistringstream并从中读取点。向量在istringstream到达时结束eof(或strin >> pt为 false)。

您需要做的是将任务operator>>(istream&, vector<Point>&)转换为方法和函数调用,然后:

  1. 声明新的方法和功能。
  2. 写下任务的描述(如>>评论中所做的那样)。
  3. 将描述转化为方法和函数调用。
  4. 重复直到没有新的方法或函数要写。

为什么 myPolygon::sides()与您的 不同Polygon::sizeOfVect():请注意,它vector::capacity返回向量可以容纳的元素数,而无需调整大小;也就是说,它基本上是 sizeof(vect)/typeof(element)。vector::size是当前存储在向量中的元素数。如果您为元素预先分配空间(例如Polygon::sides(int)确实如此),或者如果您从后面弹出元素,这两者可能会有所不同。然而,无论如何,vector::capacityvector::size

于 2010-04-03T04:56:06.670 回答