Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved overloaded function type -- array subscript overloading in C++

Tags:

c++

operators

So I have a vector class that looks a bit like this (most methods stripped out for clarity):

class D3Vector {
  private:
    double _values[3];
  public:
    const double& operator[](const int index) const;
    double& operator[](const int index);
};

double& D3Vector::operator[](const int index) {
  assert(index >= 0 && index < 3);
  return _values[index];
}

const double& D3Vector::operator[](const int index) const {
  assert(index >= 0 && index < 3);
  return _values[index];
}

And at some point in my code I call this array subscript overload as follows:

void func(D3Vector centre, double radius) {
  double limits[6];
  int i;
  for (i = 0; i < 3; i++) {
    // both these lines cause the error...
    limits[i] = centre[i] - radius;
    limits[i + 3] = centre[i] + radius;
  }
  ...
}

but I get this error at compile time:

error: invalid types '<unresolved overloaded function type>[int]' for array subscript

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.

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:

instance[i] = 5.7;

and

new_value = instance[j] + 17.3;

?

EDIT: the full class specification, as requested:

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& right);
    D3Vector operator-(const D3Vector& right);

    // unary -, reverses sign of components:
    D3Vector operator-();

    // binary *, scales components.
    D3Vector operator*(const double scale);

    // the same, as self-assignment operations:
    D3Vector& operator+=(const D3Vector& right);
    D3Vector& operator-=(const D3Vector& right);
    D3Vector& operator*=(const double scale);

    // subscript operator, for member data access.
    const double& operator[](const int index) const;
    double& operator[](const int index);

    // dot product:
    double dot(D3Vector& right);

    // cross product:
    D3Vector cross(D3Vector& right);

    // shortcut to vector length:
    double mod();

    // faster way of getting length squared:
    double mod_squared();
};
like image 819
tehwalrus Avatar asked Sep 01 '25 10:09

tehwalrus


1 Answers

As commenters are pointing out, this error pops up when you try and call a function with brackets [] instead of parentheses (). This is exactly what is happening here, and wasn't obvious because I simplified the code example.

In the question, I post and example function called func - this was in fact a constructor of an inherited class (hence, rather than post all the code, I simplified.)

The base class contains all we need to know:

class D3Shape {
  protected:
    double l[6];
    virtual void initilise_limits() = 0;
  public:
    virtual bool contains(D3Vector point) = 0;
    vector<double> limits();
};

i.e. I had confused l, the private member variable storing the double[6] I was looking for, with limits(), a function for retrieving them in a std::vector<double> container. This was complicated by the fact that I was (successfully) using my real array-subscript-overloaded class on the same line, which confused me! The compiler error "column number" on the file was in fact pointing to the first character after the =, further muddying the waters.

Many thanks to everyone who commented.

like image 98
tehwalrus Avatar answered Sep 03 '25 00:09

tehwalrus