Unresolved overloaded function type - array overloading in C ++

So, I have a vector class that looks a bit like it (most of the methods have been removed 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 index overload like this:

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 am getting this error at compile time:

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

      

Now I've fumbled with the overload function signatures, adding and removing reference characters, adding and removing a constant, but I'm really just guessing here.

What is a sane way to write overloads of array index operators for a vector class for real numbers like this, which allows us to do simple things like:

instance[i] = 5.7;

      

and

new_value = instance[j] + 17.3;

      

?

EDIT : Complete 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();
};

      

+3


source to share


1 answer


As commenters point out, this error appears when you try to call a function using parentheses []

instead of parentheses ()

. This is exactly what is happening here and was not obvious because I was simplifying the example code.

In the question, I'm posting an example function called func

- it was actually the constructor of an inherited class (hence, instead of posting all the code, I've simplified it.)

The base class contains everything 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 was confused l

, the private member variable storing double[6]

I was looking for with limits()

, a function to retrieve them in the container std::vector<double>

. This was complicated by the fact that I (successfully) used my real class with an index-overloaded class on the same line, which confused me! The compiler error "column number" in the file actually pointed to the first character after =

, which mutated the waters even more.

Many thanks to everyone who commented.

+7


source







All Articles