title: [ ]operator for custom object : '[' illegal for class?
body: <p>Here is my code where I try to access an element of class <code>Edge</code>:</p>
  
  <pre><code>#include &lt;iostream&gt; 
  // for the sort()
  #include &lt;algorithm&gt;
  #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 &amp;&amp; p0.y == p1.y) throw std::invalid_argument("Edge: Identical points!"); 
      } 
      // operator&lt; defined for the sorting by increasing ordinate of the end point
      bool operator&lt;(const Edge&amp; e){ return (end.y &lt; e.end.y); }
      // data members: start point and end point of the line
  Point start;
  Point end;
  };
  
  static void generatePoints(vector&lt;Point&gt;&amp; 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&lt;Point&gt; polyPoints;
  generatePoints(polyPoints);
  
  vector&lt;Edge&gt; polyEdges;
  Point first = polyPoints(0);
  Point last = polyPoints(polyPoints.size()-1);
  polyEdges.push_back(Edge(last, first));
  for (size_t i = 1; i &lt; polyPoints.size(); ++i) polyEdges.push_back(Edge(polyPoints[i-1], polyPoints[i]));
  
  
  int yCoordinate = polyEdges[i].end.y;
  
  return 0;
  }
  </code></pre>
  
  <p>Now, I have a vector of edges, like so:</p>
  
  <pre><code>vector&lt;Edge&gt; polyEdges;
  </code></pre>
  
  <p>and when I try to access it a specific member <code>polyEdges[i].end.y</code>, I get the following error message:</p>
  
  <pre><code>  '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
  </code></pre>
  
  <p>It must be related with the overloading of the <code>[]operator</code>.</p>
  
  <h2>Question:</h2>
  
  <p>Should I overload <code>[] operator</code> and if so, how to do it?</p>