I've this class
namespace baseUtils {
template<typename AT>
class growVector {
int size;
AT **arr;
AT* defaultVal;
public:
growVector(int size, AT* defaultVal ); //Expects number of elements (5) and default value (NULL)
AT*& operator[](unsigned pos);
int length();
void reset(int pos); //Resets an element to default value
void reset(); //Resets all elements to default value
~growVector();
};
}
and this is the implementation for operator[]
template<typename AT>
AT*& growVector<AT>::operator [](unsigned pos){
if (pos >= size){
int newSize = size*2;
AT** newArr = new AT*[newSize];
memcpy(newArr, arr, sizeof(AT)*size);
for (int i = size; i<newSize; i++)
newArr[i] = defaultVal;
size = newSize;
delete arr;
arr = newArr;
}
return arr[pos];
}
(yes I do realize i don't check if size*2 >= pos... but that's not the point now) if I use it in code like:
int main() {
growVector<char> gv();
char* x = NULL;
for (int i = 0; i< 50; i++){
gv[i] = x;
}
gv.reset();
return 0;
}
the compiler says
../src/base.cpp:98: warning: pointer to a function used in arithmetic
../src/base.cpp:98: error: assignment of read-only location ‘*(gv + ((unsigned int)i))’
../src/base.cpp:98: error: cannot convert ‘char*’ to ‘baseUtils::growVector<char>()’ in assignment
referring to the line gv[i] = x; (seems like it doesn't see the redefinition of [])
Why???? What am I missing?