I have the following classes:
class Point { private: Vector2d _coordinates; int _label; public: Point(const double x = 0.0, const double y = 0.0); Point(Vector2d coordinates); void setCoordinates(const double x, const double y) {_coordinates(x, y);} const Vector2d& getCoordinates() const {return _coordinates;} int getPointLabel() const{ return _label;} void setPointLabel(int label); class PolygonCutter { //output parameters vector<Point> _newPoints; vector< vector<Point>> _cuttedPolygons; public: void AddLabels(const vector<Point> &points, const vector<const unsigned int>&polygonVertices); };
I working on PolygonCutter. i need it to be just a manager that uses a methodAddLabels to set the member _label of each Point in vector points to the integers passed as the vector polygonVertices How can I do it? I am having trouble because I am not able to access the method setPointLabel(int label) from here:
My try contains the following errors:
void AddLabels(const vector<Point> &points, const vector<const unsigned int>&polygonVertices); { int originalNumberOfVertices = points.size(); for(int i = 0; i<originalNumberOfVertices ; i++) { int label = polygonVertices[i]; // ERROR type 'const vector<const unsigned int>' does not provide a subscript operator const Point& point = points[i]; point.setPointLabel(label); // ERROR 'this' argument to member function 'setPointLabel' has type 'const Point', but function is not marked const } }
Answer
in AddLabels method, vector points is const. setPointLabel is non-const member function. non-const member functions can only be invoked for non-const objects. Either
- remove const from const vector &points , or
- mark function setPointLabel const.