You can't. Define the class template's member functions within the header file.
Templates are C++'s primary code generation tool. When you write a template, you're writing instructions that tell the C++ compiler how to write code for you. The compiler can't follow those instructions when they're in a different file (translation unit).
Well, the short answer is that you don't define them in a .cpp file, because the compiler requires the full definition of a template to be visible when compiling a compilation unit.
But your other issue is that I don't see why you're creating a template here to begin with. What problem are you trying to solve by making your clock class a template? You have a template parameter called "time", but you never use this anywhere in the class template itself.
Also, your class template is called clock, but you seem to be trying to make a constructor called "Time", which doesn't match the name of your class.
@mbozzi so i have to declare and define them in one file? when we learned classes, we were taught to separate definition from declaration -- i thought it'd be the same when templates are thrown into the thingymajig.
@Ganado the code i provided is just an example. we're learning templates, FIFO, and LIFO. so based on your example, I need to write template <typename T> every time i define something outside the class Thing?
to have a separate cpp for the template functions
explicit template instantiation: http://www.cplusplus.com/forum/articles/14272/
caveat, you need to define all the types that you template may be used (if you try to use on another type you'll get an "undefined reference" link error)
> so i have to declare and define them in one file?
you may separate the definition in another file and simple #include it at the bottom
(consider include like copy-paste)
1 2 3 4 5 6
//foo.h
#pragma once
template <class T>
T foo(T value);
#include "foo_impl.h
1 2 3 4 5 6
//foo_impl.h
#pragma once
template <class T>
T foo(T value){
return value;
}
> I need to write template <typename T> every time i define something outside the class Thing?
yes