So I'm trying to implement a custom vector class and I'm running into compile errors. Also, my code may or may not be totally off. Any help is much appreciated.
MyVector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef __MYVECTOR_H__
#define __MYVECTOR_H__
constint DEFAULT_VECTOR_SIZE = 0;
template<typename T>
class MyVector{
private:
int vtr_size;
T *elements; // = elements[vtr_size]
public:
MyVector<T>();
MyVector<T>(int size);
//other function prototypes are here
};
#endif;
Made some fixes. Also, I'm not sure I understand why realloc is wrong here, I'm using it with elements, not new T. At the moment I'm getting errors with my function calls in main.cpp
[Linker Error] undefined reference to 'MyVector<int>::MyVector(int)'
ld (Id?) returned 1 exit status
[Build error] error 1
This is running on Dev C++ as a requirement for my class. I understand it's saying I need to implement that function, but it is clearly implemented in MyVector.cpp....
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
Oh wow, thanks so much. I would have never figured that out, didn't even know what to look for. See my reasoned logic is great, but my computer logic is pretty fuzzy still, and I don't known as much as I should to be honest. Thanks for the help!
Is this right because it's still not wanting to work....