我正在尝试为一个只有成员是矢量的类编写一个重载的流插入运算符。它是Point
s的向量,是包含两个struct
的{{1}}。
我想我想要的是将用户输入(一堆double
s)插入到流中,然后发送给修改器方法。我正在处理其他流插入示例,例如:
double
但是当我尝试类似的东西时:
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
的名字。)即使它是正确的,我实际上也不知道在我的程序中使用什么语法来使用它,我就是也不是100%确定我的修饰符方法需要看起来像什么。
这是我的vector <Point>
课程:
Polygon
抱歉这么模糊;一位讲师给了我们一个关于流插入的简短例子,然后让我们自己离开了。
答案 0 :(得分:1)
问题是没有用于向量的标准提取运算符(插入到流,从流中提取),因此您需要定义自己的。此外,默认情况下,流会跳过空格,因此您通常不需要使用std::ws
。你没有定义矢量输入是如何终止的,所以我假设换行符表示矢量的结束。
由于这是学校问题,我不能给你太多。首先,这里有一些声明供您填写,其中一些包含可能证明有用的声明。导入名称空间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
)的替代方法,您可以将下一行读入{{3的字符串然后进入getline
并从那里读取点。当istringstream
到达istringstream
(或eof
)时,向量结束。
您需要做的是将operator>>(istream&, vector<Point>&)
中的任务转换为方法和函数调用,然后:
>>
评论中所述)。为什么我的Polygon::sides()
与您的Polygon::sizeOfVect()
不同:请注意vector::capacity
会返回矢量在不调整大小的情况下可以容纳的元素数量;也就是说,它基本上是sizeof(vect)/ typeof(元素)。 vector::size
是当前存储在向量中的元素数。如果您预先为元素分配空间,例如Polygon::sides(int)
,或者如果您从后面弹出元素,则两者可能会有所不同。不管怎样,vector::capacity
≥vector::size
。