How to write a template using h and cpp ?
I have the .h and cpp files for a class where I want to write a function with a template typevar.
Code:
template <class TT_1>
void write(TT_1 value);
I have problems when I want to write the function at the cpp file. I have not auto-completion info neither tooltip info. And sometimes I have strange compile behaviors. (If I modify some in this template is not compiled, sometimes I have to clean the project ...)
Any idea ? That is , how can I write the template implemention to let QtCreator and mingw works fine ?
Thanks
Re: How to write a template using h and cpp ?
Use a language with different rules than C++.
Re: How to write a template using h and cpp ?
Read this thread, you may find it useful.
Re: How to write a template using h and cpp ?
A simple explanation comes from the template definition, a template is not a class or a function... a template is just a "pattern" that the compiler uses to generate code for classes or functions.
So for the compiler to be able to generate the code, it must see both the template definition and declaration (not just declaration).
There are some tricks: you can include the .cpp file into the .h file (others say to rename the implementation with a different extension then .cpp) but i don't see the benefits of doing that.
Re: How to write a template using h and cpp ?
File extension is meaningless, it's just for the human's benefit. A general rule of a thumb is that the compiler must read the file to use it. C/C++ compilers don't read unrelated files which compiling some other file.