• 12
• 27
• 9
• 9
• 20

External files and template functions (New Problem)

This topic is 4808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

New problem, and since it is quite relative, I'll just recycle this post. I'm having another problem with external files and template functions. This main source file is 'main.cpp', which uses 'func.h'. Here's the code:
HEADER "func.h"
template <typename objType>
long instance_create(int x, int y);

SOURCE "func.cpp"
template <typename objType>
long instance_create(int x, int y) {
//Code goes here
}


And I call it like this: ('CChar' is a class for a player) id = instance_create<CChar>(100,0); I get this linker error:
[Linker error] undefined reference to long instance_create<CChar>(int, int)'

Share on other sites
You need to define you variables with the extern keywork in all of your header files and only declare it in one of your cpp files.

If the linker is complaining, it is because it has been defined in more than one place

Share on other sites
Hm, I guess I didn't fully test it out.
Is there some easy way to declare all of them to be 'extern', like with brackets of some sort: (this doesn't work, it's just an example)
extern { int a; int b;}`

Oh boy, this is gonna be fun.

Share on other sites
Well, it turns out that in using this fun little system of templates with CPP files doesn't work. I found that the template function HAD to be included in the header file, or else it doesn't read it (which really sucks).
Does anyone know how to bypass this?

Share on other sites
there are two ways.

One I don't remember =P

The other is using the export keyword in teh source definition, but not many compilers support this (I know dev-c++ doest, but the Microsoft compilers do...)