我正在尝试使用重载的提取运算符简单地计算向量的元素。向量 contians Point,它只是一个包含两个双精度的结构。该向量是一个名为 Polygon 的类的私有成员,所以这是我的 Point.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <string>
#include <sstream>
struct Point
{
double x;
double y;
//constructor
Point()
{
x = 0.0;
y = 0.0;
}
friend std::istream& operator >>(std::istream& stream, Point &p)
{
stream >> std::ws;
stream >> p.x;
stream >> p.y;
return stream;
}
friend std::ostream& operator << (std::ostream& stream, Point &p)
{
stream << p.x << p.y;
return stream;
}
};
#endif
我的多边形.h
#ifndef POLYGON_H
#define POLYGON_H
#include "Segment.h"
#include <vector>
class Polygon
{
//insertion operator needs work
friend std::istream & operator >> (std::istream &inStream, Polygon &vertStr);
// extraction operator
friend std::ostream & operator << (std::ostream &outStream, const Polygon &vertStr);
public:
//Constructor
Polygon(const std::vector<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 vertices.size();}
//add Point elements to vector
inline void setVertices(const Point &theVerts){vertices.push_back (theVerts);}
private:
std::vector<Point> vertices;
};
和多边形.cc
using namespace std;
#include "Polygon.h"
// Constructor
Polygon::Polygon(const vector<Point> &theVerts)
{
vertices = theVerts;
}
//Default Constructor
Polygon::Polygon(){}
istream & operator >> (istream &inStream, Polygon::Polygon &vertStr)
{
inStream >> ws;
inStream >> vertStr;
return inStream;
}
// extraction operator
ostream & operator << (ostream &outStream, const Polygon::Polygon &vertStr)
{
outStream << vertStr.vertices << endl;
return outStream;
}
我认为我的点插入/提取是正确的,我可以使用它插入和输出
我想我应该能够......
cout << myPoly[i] << endl;
在我的司机?(循环)甚至......
cout << myPoly[0] << endl;
没有循环?我已经尝试了各种
myPoly.at[i];
myPoly.vertices[i];
等等等等
还尝试了我的提取功能中的所有版本
outStream << vertStr.vertices[i] << endl;
在循环内等
当我刚刚创建一个...
vector<Point> myVect;
在我的驱动程序中,我可以...
cout << myVect.at(i) << endl;
没问题。
几天来试图找到答案,真的迷失了,而不是缺乏尝试!
请原谅我缺乏评论和格式,也缺少一些零碎的东西。