c++ - [ ]operator for custom object : '[' illegal for class? -
here code try access element of class edge
:
#include <iostream> // sort() #include <algorithm> #define pi 3.14159265358979323846 struct point{ point(int xx, int yy): x(xx), y(yy) { } int x; int y; }; // class edge: representing lines segments of poly-line struct edge{ // constructor edge(point p0, point p1) : start(p0), end(p1){ if (p0.x == p1.x && p0.y == p1.y) throw std::invalid_argument("edge: identical points!"); } // operator< defined sorting increasing ordinate of end point bool operator<(const edge& e){ return (end.y < e.end.y); } // data members: start point , end point of line point start; point end; }; static void generatepoints(vector<point>& p){ p.push_back(point(50,50)); p.push_back(point(200,50)); p.push_back(point(200,200)); p.push_back(point(50,200)); } //------------------------------------------------------------------------------------------------ int main(){ // generate points poly-line vector<point> polypoints; generatepoints(polypoints); vector<edge> polyedges; point first = polypoints(0); point last = polypoints(polypoints.size()-1); polyedges.push_back(edge(last, first)); (size_t = 1; < polypoints.size(); ++i) polyedges.push_back(edge(polypoints[i-1], polypoints[i])); int ycoordinate = polyedges[i].end.y; return 0; }
now, have vector of edges, so:
vector<edge> polyedges;
and when try access specific member polyedges[i].end.y
, following error message:
'vector' : undeclared identifier 'point' : illegal use of type expression see declaration of 'point' 'p' : undeclared identifier 'generatepoints' : function-style initializer appears function definition vector' : undeclared identifier see declaration of 'point' 'polypoints' : undeclared identifier 'point' : illegal use of type expression 'polypoints' : undeclared identifier 'generatepoints': identifier not found 'vector' : undeclared identifier edge' : illegal use of type expression see declaration of 'edge' error c2088: '[' : illegal class polyedges' : undeclared identifier 'polypoints': identifier not found 'polypoints' : undeclared identifier left of '.size' must have class/struct/union 'point' : illegal use of type expression error c2228: left of '.end' must have class/struct/union error c2228: left of '.y' must have class/struct/union
it must related overloading of []operator
.
question:
should overload [] operator
, if so, how it?
try idea code. no need override [] operator
. uses [] operator
vector
class. not of class
#include <iostream> #include <vector> struct point { int x; int y; point(int xx, int yy) : x(xx), y(yy) { } }; struct edge { point start; point end; edge(point p0, point p1) : start(p0), end(p1) { if (p0.x == p1.x && p0.y == p1.y) { throw std::invalid_argument("edge: identical points!"); } } bool operator<(const edge& e) { return (end.y < e.end.y); } }; int main() { point p1(0, 1); point p2(2, 3); edge e1(p1,p2); std::vector<edge> polyedges; polyedges.push_back(e1); int = 0; std::cout << polyedges[i].end.y << std::endl; system("pause"); //output "3" }
Comments
Post a Comment