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 .cpp is
template<typename AT>
growVector<AT>::growVector(int size, AT* defaultVal) {
arr = new AT*[size];
this->size = size;
for (int i = 0 ; i < size; i++){
arr[i] = defaultVal;
}
}
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];
}
//template<typename AT>
//const AT*& growVector<AT>::operator [](unsigned pos) const{
// if (pos >= size)
// throw std::range_error("index out of range in constant vector");
// }
// return NULL;
//}
template<typename AT>
int growVector<AT>::length(){
return size;
}
template<typename AT>
growVector<AT>::~growVector(){
delete arr;
}
template<typename AT>
void growVector<AT>::reset(int pos){
if (pos>=size)
throw new std::range_error("index out of bounds");
arr[pos] = defaultVal;
}
template<typename AT>
void growVector<AT>::reset(){
for (int pos = 0; pos<size; pos++)
arr[pos] = defaultVal;
}
...very basic.
I use it in
int main() {
growVector<char> gv(5, (char*)NULL);
char* x = NULL;
for (int i = 0; i< 50; i++){
gv[i] = x;
}
gv.reset();
return 0;
}
it compiles but the linker says:
Invoking: GCC C++ Linker
g++ -pthread -o"base" ./src/base.o ./src/base/baseController.o ./src/base/baseThreads.o ./src/base/utils.o ./src/base/utils/utilClasses.o
./src/base.o: In function `main':
/home/dario/workspace/base/Debug/../src/base.cpp:95: undefined reference to `baseUtils::growVector<char>::growVector(int, char*)'
/home/dario/workspace/base/Debug/../src/base.cpp:98: undefined reference to `baseUtils::growVector<char>::operator[](unsigned int)'
/home/dario/workspace/base/Debug/../src/base.cpp:100: undefined reference to `baseUtils::growVector<char>::reset()'
/home/dario/workspace/base/Debug/../src/base.cpp:101: undefined reference to `baseUtils::growVector<char>::~growVector()'
/home/dario/workspace/base/Debug/../src/base.cpp:101: undefined reference to `baseUtils::growVector<char>::~growVector()'
collect2: ld returned 1 exit status
I'm really clueless