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

时间:2015-09-30 08:18:24

标签: c++ operator-overloading

这是我的代码,我尝试访问类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,如果是,那该怎么办?

1 个答案:

答案 0 :(得分:2)

尝试从此代码中获得一些想法。您无需覆盖[] operator。它使用[] operator类中的vector。不是来自任何一个班级

#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"
}