超载<<打印出 std::list 的运算符

发布于 2024-11-29 07:12:51 字数 1252 浏览 1 评论 0 原文

我在尝试实现重载的 << 时遇到了一些问题。可以打印出 std::list 的运算符函数,它是我的一个类的成员。该类如下所示:

class NURBScurve {
  vector<double> knotVector;
  int curveOrder;
  list<Point> points;
public:
  /* some member functions */
  friend ostream& operator<< (ostream& out, const NURBScurve& curve);
};

我感兴趣的关键成员变量是“点”列表 - 这是我创建的另一个类,它存储点的坐标以及关联的成员函数。当我尝试实现重载的 <<运算符功能为:

ostream& operator<<( ostream &out, const NURBScurve &curve)
{
 out << "Control points: " << endl;
 list<Point>::iterator it;
 for (it = curve.points.begin(); it != curve.points.end(); it++)
    out << *it; 
 out << endl;
 return out;
}

我开始遇到问题。具体来说,我收到以下错误: 错误:

no match for ‘operator=’ in ‘it = curve->NURBScurve::points. std::list<_Tp, _Alloc>::begin [with _Tp = Point, _Alloc = std::allocator<Point>]()’
/usr/include/c++/4.2.1/bits/stl_list.h:113: note: candidates are: std::_List_iterator<Point>& std::_List_iterator<Point>::operator=(const std::_List_iterator<Point>&)

我在这里有点困惑,但我相信这与我正在使用的列表迭代器有关。我对 curve.points.begin() 的表示法也不太有信心。

如果有人能阐明这个问题,我将不胜感激。我现在已经盯着这个问题太久了!

I've been having some problems trying to implement an overloaded << operator function that can print out a std::list which is a member of one of my classes. The class looks like this:

class NURBScurve {
  vector<double> knotVector;
  int curveOrder;
  list<Point> points;
public:
  /* some member functions */
  friend ostream& operator<< (ostream& out, const NURBScurve& curve);
};

The key member variable I am interested in is the list of "Points" - this is another class that I have created which stores coordinates of a point along with associated member functions. When I try and implement the overloaded << operator function as:

ostream& operator<<( ostream &out, const NURBScurve &curve)
{
 out << "Control points: " << endl;
 list<Point>::iterator it;
 for (it = curve.points.begin(); it != curve.points.end(); it++)
    out << *it; 
 out << endl;
 return out;
}

I start to get problems. Specifically, I get the following error:
error:

no match for ‘operator=’ in ‘it = curve->NURBScurve::points. std::list<_Tp, _Alloc>::begin [with _Tp = Point, _Alloc = std::allocator<Point>]()’
/usr/include/c++/4.2.1/bits/stl_list.h:113: note: candidates are: std::_List_iterator<Point>& std::_List_iterator<Point>::operator=(const std::_List_iterator<Point>&)

I'm a bit stumped here, but I believe it has something to do with the list iterator I am using. I'm also not too confident with the notation of curve.points.begin().

If anyone can shed some light on the problem I would appreciate it. I'm at the point where I've been staring at the problem for too long!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-12-06 07:12:51

curve 是 const 限定的,因此 curve.points 是 const 限定的,并且 curve.points.begin() 返回一个 std: :list::const_iterator,而不是 std::list::iterator

容器有两个 begin()end() 成员函数:一对不是 const 限定成员函数并返回 iterator,另一对不是 const 限定成员函数并返回迭代器对是 const 限定的并返回 const_iterator。通过这种方式,您可以迭代一个非 const 的容器,并读取和修改其中的元素,但您也可以迭代具有只读访问权限的 const 容器。

curve is const-qualified, so curve.points is const-qualified and curve.points.begin() returns a std::list<Point>::const_iterator, not a std::list<Point>::iterator.

Containers have two begin() and end() member functions: one pair are not const-qualified member functions and return iterators, the other pair are const-qualified and return const_iterators. This way you can iterate over a container that is not const and both read and modify the elements in it, but you can also iterate over a const container with read-only access.

因为看清所以看轻 2024-12-06 07:12:51

或者,

您可以使用 std::copy 作为:

std::copy(points.begin(), points.end(), 
                      std::ostream_iterator<Point>(outStream, "\n"));

确保 operator<< 的签名是这样的:

std::ostream & operator <<(std::ostream &out, const Point &pt);
                                            //^^^^^ note this

Or,

You could use std::copy as:

std::copy(points.begin(), points.end(), 
                      std::ostream_iterator<Point>(outStream, "\n"));

Make sure the signature of operator<< is this:

std::ostream & operator <<(std::ostream &out, const Point &pt);
                                            //^^^^^ note this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文