Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带向量的重载流插入运算符_C++_Vector_Operator Overloading_Insertion - Fatal编程技术网

C++ 带向量的重载流插入运算符

C++ 带向量的重载流插入运算符,c++,vector,operator-overloading,insertion,C++,Vector,Operator Overloading,Insertion,我试图为一个类编写一个重载流插入操作符,该类的唯一成员是向量。它是点s的向量,它是一个包含两个doubles的结构。 我想我想要的是将用户输入(一堆doubles)插入到流中,然后发送到修改器方法。我正在研究其他流插入示例,例如: std::ostream& operator<< (std::ostream& o, Fred const& fred) { return o << fred.i_; } 我得到一个错误“不匹配运算符>>等”

我试图为一个类编写一个重载流插入操作符,该类的唯一成员是向量。它是点s的向量,它是一个包含两个doubles的结构。 我想我想要的是将用户输入(一堆
double
s)插入到流中,然后发送到修改器方法。我正在研究其他流插入示例,例如:

std::ostream& operator<< (std::ostream& o, Fred const& fred)
 {
   return o << fred.i_;
 }
我得到一个错误“不匹配
运算符>>
等”。如果我关闭
顶点,它会编译,但我认为这不对。(顺便说一句,
顶点
是我的
向量
的名称),即使它是正确的,我也不知道在我的程序中使用什么语法来使用它,而且我也不能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;
 }
//头
#ifndef多边形
#定义多边形
#包括“h段”
#包括
类多边形
{
friend std::istream&operator>>(std::istream&inStream,Polygon&vertStr);
公众:
//建造师
多边形(常量点和顶点);
//默认构造函数
多边形();
//复制构造函数
多边形(常量多边形和多边形副本);
//访问器/修改器方法
内联std::vector getVector()常量{返回顶点;}
//返回向量元素的数目
内联int-sizeOfVect()常量{return(int)vertices.capacity();}
//将点元素添加到向量
内联void集合顶点(常量点和顶点){texts.push_back(顶点);}
私人:
向量顶点;
};
#恩迪夫
//身体
使用名称空间std;
#包括“Polygon.h”
//建造师
多边形::多边形(常量点和顶点)
{
顶点。向后推(顶点);
}
//复制构造函数
多边形::多边形(常量多边形和多边形副本)
{
顶点=多边形复制。顶点;
}
//默认构造函数
多边形::多边形(){}
istream和运算符>>(istream和inStream、多边形和顶点)
{
流内>>ws;
河道内>>vertStr;
回流河道;
}

抱歉说得这么含糊;一位讲师刚刚给了我们一个简单的流插入示例,然后让我们自己做。

问题是向量没有标准的提取操作符(插入到流,从流中提取),所以您需要定义自己的。此外,默认情况下,流跳过空白,因此通常不需要使用
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
    ...
}
#包括
//如果ch是水平空格,则返回true。区域设置有点棘手,
//因此,您现在可以跳过它们,而是从定义的函数开始
//在标题中
bool-ishs(char-ch,std::locale-loc=std::locale::global());
//如果ch是垂直空格,则返回true
bool-isv(char-ch);
//如果流“in”中的下一个字符是垂直空格,则返回true。
bool下线(标准::istream和in);
//读取并丢弃水平空格
std::istream&hs(std::istream&in);
类点{
公众:
//Scalar是,因此您可以使用“Point::Scalar”而不是“double”,
//使更改点的类型变得容易。
//当您已经介绍了模板时,这将使点模板化变得很简单。
typedef双标量;
...
};
类多边形{
公众:
//添加pt作为此多边形的最后一个顶点
//注意:这基本上是您的“设置顶点”具有不同的名称
多边形和附加(常量点和pt);
//将从“开始”到“结束”的点添加到此多边形
模板
多边形和附加(_iterstart,_iterend);
//删除此多边形中的所有点
无效擦除();
//返回此多边形上的边数,
//这也是顶点的数量
//注意:这与您的“sizeOfVect”不同;更多信息,请参见下文
int sides()常量{返回顶点.size();}
//为多边形留出空间,使其具有“s”边。
空边(整数s){顶点。调整大小(s);}
}
/*将当前行上的下两个数字读入pt。
如果只有一个数字,则引发异常。
*/
std::istream&operator>>(std::istream&in,Point&pt);
/*将当前行上的数字读入“多边形”上的点。
如果只有一个数字,则引发异常。更可取地
如果有奇数,则不会更改“poly”。
您还可以将此运算符的内容放入>>(istream&,Polygon&)
*/
std::istream&operator>>(std::istream&in,std::vector&Vertexts){
std::列表点;
点pt;
//当不在下线(in)时,读入pt并将其添加到点
//之后,清空顶点,然后将点中的点添加到顶点
...
}
作为字符相关函数(
ishs
ISV
hs
eol
)的替代方法,您可以将下一行读入字符串,然后读入,并从中读取点。当
istringstream
到达(或)时,向量结束

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

  • 声明新的方法和函数
  • 写下任务的描述(如
    >
    注释中所述)
  • 将描述转换为方法和函数调用
  • 重复此操作,直到没有新的方法或函数可编写为止
  • 为什么我的
    Polygon::sides()
    不同于您的
    Polygon::sizeOfVect()
    :请注意
    vector::capacity
    返回向量在不调整大小的情况下可以容纳的元素数;也就是说,它基本上是sizeof(vect)/typeof(element)
    vector::size
    是当前存储在向量中的元素数。两个可能
    #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
        ...
    }