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

10,369

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.

Share:
10,369
tehwalrus
Author by

tehwalrus

I have worked for a few different companies on top of many stacks including ActionScript 2 and 3, C# (.NET 1.1-3.5, windows desktop and ASP.NET, .NET 4.6 C#/WPF), MS SQL/MariaDB/PostgreSQL, PHP, Java, Python, Cython, C and C++ (and C++-compatible C, and C-compatible C++), Objective C. I've worked on Windows, macOS and Linux (and applications targeting all three.) My most proficient programming language is Python, which I used (along with Cython/C/C++ extensions) to complete my PhD in physics, in which I produced a new computational geometry algorithm. I have written a Lisp, made a flash game, published an android app and used OpenGL as a debugger. Now I work for a hardware vendor in Cambridgeshire where I mostly write C#/XAML (although I occasionally use Python there too.)

Updated on June 08, 2022

Comments

  • tehwalrus
    tehwalrus almost 2 years

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