Search code examples
c++arrayspointerstypesdynamic-arrays

Field has an incomplete type: int*[]


class NumberArray {

int *nums []; // this line gets error for incomplete type int *[]
int size_;

public:
    NumberArray(const int i){nums = new int[i]; size_ = i;}
    ~NumberArray(){delete [] *nums;}

    void set(int i, int n){(*nums)[i] = n;}
    int get(int i){return (*nums)[i];}
    int min();
    int max();
    double avg();
};

I realize this is something really simple and I'm missing it. Please enlighten me.


Solution

  • To the type system, a dynamically sized array is more or less equivalent to a pointer to the first element. That's how you use it in the constructor too.

    Change the declaration to:

    int *nums;
    

    Then there's the best practice of not using plain arrays at all, but instead use std::vector which provides better and safer operations.