Was recently trying to implement a templated class.
I initially had a header and implementation file but find that only unless the functions are implemented within the class itself does it compile and link properly.
Any ideas on why this is so? Is there a way to keep the definitions and declarations in separate files?
C++ class template function declaration and definition
No, that is not possible (at least gcc cannot do that).
gcc will compile the template multiple times: Once for each parameter of the template. If the declaration and the implementation were in different files gcc would not know which parameter types are required when compiling the implementation file.
gcc will compile the template multiple times: Once for each parameter of the template. If the declaration and the implementation were in different files gcc would not know which parameter types are required when compiling the implementation file.
I use a trick around this:
Create a standard header file. This declares your template class. Have it #include your implementation header file (Mabey the last line in the file?). This header defines your template class routines. Create a source file that #includes the main header file, and it should work fine.
A little messy, but it allows us to separate interface from implementations for template classes.
Create a standard header file. This declares your template class. Have it #include your implementation header file (Mabey the last line in the file?). This header defines your template class routines. Create a source file that #includes the main header file, and it should work fine.
A little messy, but it allows us to separate interface from implementations for template classes.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Just wanted to add this: You can instantiate member functions or the whole class for one particular type explicitly, via *drums* explicit template instantiation. For a whole class that looks like this:Neo wrote:You can have a little bit of magic by first defining a generic (void *-based, for example) container class with separately compiled implementation [...]
Code: Select all
template class Vector<void*>;