Sign in to follow this  

External files and template functions (New Problem)

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

If you intended to correct an error in the post then please contact us.

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)'
I don't understand why I get this for this and other template functions. OLD POST: "Organizing C++ Files: 'extern' Keyword..." I have looked at the tutorial here on GameDev.net on organizing files, but I'm still unclear on one part: global variables. I tried different combinations: header with 'extern', source with regular; header with regular, source with 'extern', but my linker (not compiler) still complains that I have multiple definitions of those variables. I do use the macros to make sure that headers are not included twice in the same object file, but still it complains... The problem might be that my #include files are intertwined, but without that 'intertwinsion' they would simply not work. This is confusing, and I've looked at other projects by other people, and I didn't see the 'extern' keyword anywhere, just a variable declaration in the header, the header included in the paired source, and that same header included in the main source. USING: Dev-C++ 4.9.9.1 (w/mingw), Windows XP Home Edition SP2 I have the current project at: [u]http://www.deadimp.org/downloads/DGI.zip[/u] And to give warning, it is still quite unorganized, along with the intertwined part. NOTE: Try to ignore the licensing part of the readme [Edited by - deadimp on January 16, 2005 8:43:38 PM]

Share this post


Link to post
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 this post


Link to post
Share on other sites
Hm, I guess I didn't fully test it out.
Removing download.
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this