Strange template "redefinition" linker error.

Discussion in 'C++' started by Robbie Hatley, Jun 28, 2006.

  1. I'd always thougth that a C++ compiler/linker should be able to
    instantiate a template in mulitple places (say, in two different
    translation units), even using the same template parameters so
    that the resulting functions or classes are identical, without
    causing any errors. Or so I thought.

    But a few days ago, I added a couple of new template functions
    to one of my personal library modules, and I got this linker
    error message (from memory, as near as I can recall):

    Linker Error: template std::string ConvertFromFfblk<std::string>
    (ffblk const & FileBlock) redefined in MyProgram.cpp.
    See original definition in rhutil.cpp.

    The only place this template is defined in in a header, rhutil.h,
    which is included in both translation units.

    I was able to get the program to link by declaring the template
    as "inline" in rhutil.h and recompiling rhutil.o before attempting
    to recompile my program.

    Is it normally necessary to declare a template function in a header
    as being "inline"? Or is it just something in this particular case
    that makes it necessary?

    Perhaps the fact that a library module (a *.o file embedded in a
    *.a file) instantiates this template has something to do with it?
    If the compiler first compiles the non-library object module(s)
    for a program (instantiating a template in the processs), then
    attempts to link to a pre-exising library object module which
    already contains an identical instantiation, could this cause
    problems? Or should the compiler and linker be able to handle
    that?


    --
    Robbie Hatley
    Tustin, CA, USA
    lonewolfintj atsign pacbell period net
    home period pacbell period net slantbar earnur slantbar
    Robbie Hatley, Jun 28, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Attila Feher

    Template redefinition problem

    Attila Feher, Sep 22, 2003, in forum: C++
    Replies:
    5
    Views:
    2,168
    tom_usenet
    Sep 22, 2003
  2. Derek Baker

    Strange Linker Error

    Derek Baker, May 27, 2004, in forum: C++
    Replies:
    3
    Views:
    311
    Derek Baker
    May 28, 2004
  3. Master of Puppets
    Replies:
    4
    Views:
    5,463
    Master of Puppets
    Apr 9, 2005
  4. Robbie Hatley
    Replies:
    1
    Views:
    508
    Victor Bazarov
    Jun 28, 2006
  5. Robbie Hatley
    Replies:
    2
    Views:
    707
    Ian Collins
    Jun 29, 2006
Loading...

Share This Page