0

im just getting my head around container classes and templates, and am having trouble working out how to implement them properly.. all of my problems are pretty similar, so once i get this sorted, i think i should be okay..

here is the code for the push_back member function that i am trying to implement:

//data is of type boost::ptr_vector<T>

template <class T>
void P_VContainer<T>::push_back(T* item)
{
    data.push_back(item);
}

this is how it is defined in p_vcontainer.h:

void push_back(T* item);

this is the error im getting from the compiler:

main.cpp:(.text+0x89f): undefined reference to `P_VContainer<Customer>::push_back(Customer*)'

this is how i am implementing it in main:

P_VContainer<Customer> myvector;

Customer *a = new Customer("C004", "Testy McTestington");

myvector.push_back(a);

any ideas as to what im doing wrong?

guskenny83
  • 1,321
  • 14
  • 27

1 Answers1

1

Do not divide your templates to declaration and implementation. Implement them right in header files.

Ivan Ishchenko
  • 1,468
  • 9
  • 12
  • that worked, thanks.. is there a particular reason why you shouldnt divide templates into declaration and implementation but its okay with other classes? – guskenny83 Oct 19 '13 at 10:53
  • 1
    @guskenny83: Templates must (usually) be defined in every source file that uses them, since the definition is needed to instantiate them. That (usually) means the definition must be in a header to make it available everywhere it's needed. See the duplicate for more details. – Mike Seymour Oct 19 '13 at 11:01