jeudi 31 mars 2016

"undefined reference to" linker error while working with templates [duplicate]

This question already has an answer here:

I have a class Array

template <typename T>
class Array : public SequentialContainer<T>{
public:
    Array(Int size){local_vector.reserve(size);}
    Array(std::initializer_list<T> initializer_list){
        local_vector.assign(initializer_list);
    }
    virtual Boolean contains(T &object) const;
    virtual Boolean contains(Container<T> &container) const;
    virtual Int size() const;
    virtual T &operator[](Int idx);
    virtual T &get(Int idx);
    virtual void set(Int idx, const T &object);
    virtual Int indexOf(T &object);
    virtual Iterator<T> iterator() const;
};

All the methods are implemented like this in the Array.cpp file:

template <typename T>
Boolean Array<T>::contains(T &object) const {
//code
}

If i try to use this Array class in a main.cpp file:

Array<int> c = {1, 2, 3, 4, 5, 6};
std::cout << c.[4] <<std::endl;

I am getting these linker errors:

undefined reference to `Array<int>::iterator() const'
undefined reference to `Array<int>::contains(int&) const'
undefined reference to `Array<int>::contains(Container<int>&) const'
etc...

for every single method of the Array class. All the files are in my cmake file and should be compiled. Why I am getting this linker error? could someone explain me this please?

Aucun commentaire:

Enregistrer un commentaire