title: unresolved overloaded function type -- array subscript overloading in C++
body: <p>So I have a vector class that looks a bit like this (most methods stripped out for clarity):</p>
  
  
  
  <pre><code>class D3Vector {
    private:
      double _values[3];
    public:
      const double&amp; operator[](const int index) const;
      double&amp; operator[](const int index);
  };
  
  double&amp; D3Vector::operator[](const int index) {
    assert(index &gt;= 0 &amp;&amp; index &lt; 3);
    return _values[index];
  }
  
  const double&amp; D3Vector::operator[](const int index) const {
    assert(index &gt;= 0 &amp;&amp; index &lt; 3);
    return _values[index];
  }
  </code></pre>
  
  <p>And at some point in my code I call this array subscript overload as follows:</p>
  
  
  
  <pre><code>void func(D3Vector centre, double radius) {
    double limits[6];
    int i;
    for (i = 0; i &lt; 3; i++) {
      // both these lines cause the error...
      limits[i] = centre[i] - radius;
      limits[i + 3] = centre[i] + radius;
    }
    ...
  }
  </code></pre>
  
  <p>but I get this error at compile time:</p>
  
  <pre><code>error: invalid types '&lt;unresolved overloaded function type&gt;[int]' for array subscript
  </code></pre>
  
  <p>Now, I have fiddled around with the signatures of the overload functions, adding and removing the reference symbols, adding and removing const, but I'm really just guessing here.</p>
  
  <p>What is the sensible way to write the array subscript operator overloads for a vector class for real numbers like this which allow us to do simple things like:</p>
  
  
  
  <pre><code>instance[i] = 5.7;
  </code></pre>
  
  <p>and</p>
  
  
  
  <pre><code>new_value = instance[j] + 17.3;
  </code></pre>
  
  <p>?</p>
  
  <p><strong>EDIT</strong>: the full class specification, as requested:</p>
  
  <pre><code>class D3Vector {
    private:
      double _values[3];
    public:
      // constructors - no args inits to 0.0
      D3Vector();
      D3Vector(const double x, const double y, const double z);
  
      // binary + and -:
      D3Vector operator+(const D3Vector&amp; right);
      D3Vector operator-(const D3Vector&amp; right);
  
      // unary -, reverses sign of components:
      D3Vector operator-();
  
      // binary *, scales components.
      D3Vector operator*(const double scale);
  
      // the same, as self-assignment operations:
      D3Vector&amp; operator+=(const D3Vector&amp; right);
      D3Vector&amp; operator-=(const D3Vector&amp; right);
      D3Vector&amp; operator*=(const double scale);
  
      // subscript operator, for member data access.
      const double&amp; operator[](const int index) const;
      double&amp; operator[](const int index);
  
      // dot product:
      double dot(D3Vector&amp; right);
  
      // cross product:
      D3Vector cross(D3Vector&amp; right);
  
      // shortcut to vector length:
      double mod();
  
      // faster way of getting length squared:
      double mod_squared();
  };
  </code></pre>