templated functions and multiple definitions in obj files

J

john_owens

I have a templated function called splitAndSegment (templated on the
datatype). It's declared in a header file. I then include that header
file in two separate cpp files and link them separately. Both files
use the function, and both use the same instantiation of the function
(datatype int). Thus they both instantiate the function in their
separate obj files. I declare the function inline to prevent linking
problems, but I get linking problems anyway.

template<typename T>
inline
void splitAndSegment(T * in, unsigned int * temp1, T * temp2,
Flag * ihflags, Flag * ihtflags,
const Flag * cmpflags)
{ definition here ... }

Linking errors:

Linking...
test_app.obj : error LNK2005:
__ZZ15splitAndSegmentIiEvPT_PjS1_PiS3_PKiEs_0 already defined in
sort_app.obj
test_app.obj : error LNK2005:
__ZZ15splitAndSegmentIiEvPT_PjS1_PiS3_PKiEs already defined in
sort_app.obj
test_app.obj : warning LNK4006:
__ZZ15splitAndSegmentIiEvPT_PjS1_PiS3_PKiEs_0 already defined in
sort_app.obj; second definition ignored
test_app.obj : warning LNK4006:
__ZZ15splitAndSegmentIiEvPT_PjS1_PiS3_PKiEs already defined in
sort_app.obj; second definition ignored

I'm totally puzzled and would appreciate any help on figuring out why
this occurs. It's on MS Dev Studio v. 7.10.3077 (I believe it's VS
2003).

JDO
 
G

Gianni Mariani

I have a templated function called splitAndSegment (templated on the
datatype). It's declared in a header file. I then include that header
file in two separate cpp files and link them separately. Both files
use the function, and both use the same instantiation of the function
(datatype int). Thus they both instantiate the function in their
separate obj files.
....
I'm totally puzzled and would appreciate any help on figuring out why
this occurs. It's on MS Dev Studio v. 7.10.3077 (I believe it's VS
2003).

This would be a bug in your linker. The linker is supposed to eliminate
duplicate instantiations.

One ugly work around is to place the function in an anonymous namespace.
There are probably others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top