我在尝试实现重载的 << 时遇到了一些问题。可以打印出 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!
发布评论
评论(2)
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, socurve.points
is const-qualified andcurve.points.begin()
returns astd::list<Point>::const_iterator
, not astd::list<Point>::iterator
.Containers have two
begin()
andend()
member functions: one pair are not const-qualified member functions and returniterator
s, the other pair are const-qualified and returnconst_iterator
s. 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.或者,
您可以使用
std::copy
作为:确保
operator<<
的签名是这样的:Or,
You could use
std::copy
as:Make sure the signature of
operator<<
is this: