Public Group

# External files and template functions (New Problem)

This topic is 5025 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...)

##### Share on other sites
Are you sure you don't remember (PLEASE) *whine*?

On a side note: Everything works now, and it's... better. This is good since it works, bad because the syntax is really screwey, and really really odd.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 77
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009584
• ### Who's Online (See full list)

There are no registered users currently online

×