Templates

Alright, I've got a .h file with 3 class templates in it and a bunch of prototyped methods. I'm just wondering how I declare these methods in a .cc (or .cpp) file.
Would I put

template <class T>

before all the declarations? I really just have no idea.

Thanks
You should be able to include them by just putting an include statement at the top of your source file. For example if your template file is called Templates.h then you would put

#include "Templates.h"

That is assuming that you are using an IDE for your compiler. The exact method of how you get it to recognize the other file may vary slightly depending on how yours links them. (For example in Dev-C++, it will give you a 'file not found' error unless they're under the same project. Others may just ask that you supply a directory so the compiler knows where to find the file.) I recommend reading the help file for your IDE to find out how yours works.

If you're not using an IDE, get with the times! Just kidding. The way to accomplish this in a standard compiler is by using the make utility to establish file dependencies. Having never used it myself, I'm not very familiar with this function. (The only reason I know it exists is because of a tutorial I read on another site that mentioned it breifly, but didn't go into details.)

Figured it out. For some reason, in Unix, which is what I use to write, debug, compile, and make all my files, you don't include the .h file in the .cc file. So no:
#include "Templates.h"
You actually include the .cc (or .cpp) file at the end of the header. So this:
#include "Templates.cc"
at the end of the .h file.

Weird... thanks anyways, though!
Last edited on
It is dangerous to include .cc files!

In this particular case, the problem comes because the Templates.cc file should not be a separate file. It should be part of Templates.h.

The reason is that template stuff doesn't generate any code until used. In other words, if you were to try to compile Templates.cc you'd basically get an empty object file.

Hope this makes sense.
Topic archived. No new replies allowed.