Reputation:
this:
Polygon::Polygon(const Polygon & polygon) {
for (vector<Point*>::iterator it = polygon._points.begin(); it != _points.end(); it++)
_points.push_back(*it);
}
gives me an error of
C2440 'initializing': cannot convert from 'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<Point *>>>' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<Point *>>>
While this works:
Polygon::Polygon(const Polygon & polygon) {
vector<Point*> points = polygon._points;
for (vector<Point*>::iterator it = points.begin(); it != _points.end(); it++)
_points.push_back(*it);
}
Upvotes: 0
Views: 470
Reputation: 22020
You're taking a const Polygon
as an argument. On the first case, your iterator is therefore const. On the second case, you're assigning the const polygon.vector into a local, non-const vector, which makes it possible to get a non-const iterator.
Take a look at what your code is actually doing: you're taking a polygon that you promise not to modify, and trying to insert points into it. Since that's what you're doing, better drop the const from the argument.
Upvotes: 1