1

I am making my own vector class.

.h:

template<typename T>
class MyVector
{
private:
    T *elements;
    int elementCount;
public:
    MyVector();
    MyVector(int size);
    void push_back(T value);
    void pop_back();
    int size();
    T at(int index);
    bool empty();
    void clear();
    void swap(MyVector v2);
};

.cpp:

template<typename T>
MyVector<T>::MyVector()
{
    elementCount = 0;
    elements = new int[elementCount];
    elements = (int *) realloc (elements, elementCount * sizeof(int));
}

main.cpp:

#include "MyVector.h"

int main()
{
    MyVector<char> myTestVector;   
    return 0;
}

I am getting an error when trying to simply create a MyVector object, the error is :

MyVector::MyVector(), referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I originally wrote the class to work with an already specified type, now I need it to work with any given type.

Why am I getting this error? Thanks in advance!

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
user906357
  • 4,575
  • 7
  • 28
  • 38
  • 1
    Begs the question, why are you developing your own vector? Maybe this is just for fun or practice, but maybe it's an XY Problem. – John Dibling Oct 07 '13 at 17:50
  • 4
    When using C++ templates, all the code needs to be in the header. – hamon Oct 07 '13 at 17:50
  • 1
    **Why** do everyone say that with templates **all** the code needs to be in the **header**? Do people really know what is required and just oversimplify (I fear this is not the case)? Or is it that people just don't understand it and just repeats what they sometime heard? – David Rodríguez - dribeas Oct 07 '13 at 18:18
  • @DavidRodríguez-dribeas: I'm going to bet on the latter. – John Dibling Oct 07 '13 at 18:59
  • @JohnDibling: That is my fear. I have been trying to explain this in so many comments I lost track (and got tired in the meantime) – David Rodríguez - dribeas Oct 07 '13 at 19:29
  • @DavidRodríguez-dribeas: Perhaps I should post a question to give a little more opportunity for a reasonably complete answer. Does: "Can I define templates in a source file instead of a header? If so, what steps do I have to take to prevent linking errors?" ...seem about adequate? – Jerry Coffin Oct 07 '13 at 19:38
  • @JerryCoffin: That is a good idea, you can provide an answer that not only answers that but also explain the (limited) advantages and alternatives (C++11 *template instantiation declarations* come to mind) – David Rodríguez - dribeas Oct 07 '13 at 20:15
  • @DavidRodríguez-dribeas: Doing a bit of looking, it seems like the question's already been asked several times -- though the answers are (almost?) all wrong to at least some degree. Currently contemplating whether it makes more sense to ask the question again, or answer an existing question, or possibly do some editing to generalize a current question, then try to give a general answer. – Jerry Coffin Oct 07 '13 at 20:57
  • @Jerry: I tackled this a long time ago. Tried to at least. – John Dibling Oct 07 '13 at 23:26

1 Answers1

5

C++ templates must have their definitions in the header file. This is because the compiler generates object files for every instanced type at compile time. If you move your declarations to the .h file, your code should link ok.

See here for a decent overview on how/why templates work.

ctrl-j
  • 66
  • 3