[ ] 自定义对象的运算符:'[' 对于 class 是非法的?

[ ]operator for custom object : '[' illegal for class?

这是我的代码,我尝试访问 class Edge:

的一个元素
#include <iostream> 
// for the 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 the 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 for the sorting by increasing ordinate of the end point
    bool operator<(const Edge& e){ return (end.y < e.end.y); }
    // data members: start point and end point of the 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 for the 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));
for (size_t i = 1; i < polyPoints.size(); ++i) polyEdges.push_back(Edge(polyPoints[i-1], polyPoints[i]));


int yCoordinate = polyEdges[i].end.y;

return 0;
}

现在,我有一个边向量,如下所示:

vector<Edge> polyEdges;

当我尝试访问特定成员 polyEdges[i].end.y 时,我收到以下错误消息:

  'vector' : undeclared identifier
  'Point' : illegal use of this type as an expression
  see declaration of 'Point'
  'p' : undeclared identifier
  'generatePoints' : function-style initializer appears to be a function definition
  vector' : undeclared identifier
  see declaration of 'Point'
  'polyPoints' : undeclared identifier
  'Point' : illegal use of this type as an expression
  'polyPoints' : undeclared identifier
  'generatePoints': identifier not found
  'vector' : undeclared identifier
  Edge' : illegal use of this type as an expression
  see declaration of 'Edge'
  error C2088: '[' : illegal for class
  polyEdges' : undeclared identifier
  'polyPoints': identifier not found
  'polyPoints' : undeclared identifier
  left of '.size' must have class/struct/union
  'Point' : illegal use of this type as an expression
  error C2228: left of '.end' must have class/struct/union
  error C2228: left of '.y' must have class/struct/union

肯定是跟[]operator重载有关。

问题:

我应该重载 [] operator 吗?如果是,该怎么做?

试着从这段代码中得到一些想法。您无需覆盖 [] operator。它使用 vector class 中的 [] operator。不是来自您的任何 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 i = 0;
    std::cout << polyEdges[i].end.y << std::endl;
    system("pause");

    //output is "3"
}