specializing a template function in a class

Discussion in 'C++' started by John, Aug 14, 2010.

  1. John

    John Guest

    Hi,

    I have the code below and am using gcc version 4.4.4, When I compile
    only main.cpp I get no error, but when I compile both main.cpp and
    ClassA.cpp, I get the following

    >g++ main.cpp ClassA.cpp

    /tmp/cc8inIcc.o: In function `bool ClassA::Test<bool>(bool&)':
    ClassA.cpp:(.text+0x0): multiple definition of `bool
    ClassA::Test<bool>(bool&)'
    /tmp/ccC10DId.o:main.cpp:(.text+0x0): first defined here
    collect2: ld returned 1 exit status

    Can someone explain why I am getting this error and how to solve it?

    Thanks,
    John



    == FILE main.cpp ==

    #include "ClassA.hpp"

    int main()
    {
    bool data;
    bool has = ClassA::Test(data);

    return 0;
    }


    == FILE ClassA.hpp ==
    #ifndef __CLASSA_HPP__
    #define __CLASSA_HPP__

    class ClassA
    {
    public:
    template <typename T> static bool Test(T& data);
    };

    template <typename T> bool ClassA::Test(T& data)
    { return true; }

    template <> bool ClassA::Test(bool& data)
    { return false; }

    #endif

    == FILE ClassA.cpp ==
    #include "ClassA.hpp"

    int abc_;
    John, Aug 14, 2010
    #1
    1. Advertising

  2. John

    aryan Guest

    Try moving the below part of code to ClassA.cpp

    template <typename T> bool ClassA::Test(T& data)
    { return true; }

    template <> bool ClassA::Test(bool& data)
    { return false; }
    aryan, Aug 14, 2010
    #2
    1. Advertising

  3. John

    Jonathan Lee Guest

    On Aug 14, 2:10 pm, John <> wrote:
    > Hi,
    >
    > I have the code below and am using gcc version 4.4.4, When I compile
    > only main.cpp I get no error, but when I compile both main.cpp and
    > ClassA.cpp, I get the following

    [snip]
    > Can someone explain why I am getting this error and how to solve it?


    The template specialization violates the One Definition Rule. In
    other words, you are defining

    ClassA::Test<bool>(bool& data)

    twice (once when the header is included via main, and once when
    it is included in ClassA.cpp).

    Throw an "inline" in there and you'll get the appropriate linkage:

    template <> inline bool ClassA::Test(bool& data)
    { return false; }

    The "extern inline" linkage basically says 'yeah, I'm defining
    the function twice, but its exactly the same thing, so don't
    fret'.

    --Jonathan
    Jonathan Lee, Aug 14, 2010
    #3
  4. John

    James Kanze Guest

    On Aug 14, 8:10 pm, John <> wrote:

    > I have the code below and am using gcc version 4.4.4, When
    > I compile only main.cpp I get no error, but when I compile
    > both main.cpp and ClassA.cpp, I get the following


    > >g++ main.cpp ClassA.cpp


    > /tmp/cc8inIcc.o: In function `bool ClassA::Test<bool>(bool&)':
    > ClassA.cpp:(.text+0x0): multiple definition of `bool
    > ClassA::Test<bool>(bool&)'
    > /tmp/ccC10DId.o:main.cpp:(.text+0x0): first defined here
    > collect2: ld returned 1 exit status


    > Can someone explain why I am getting this error and how to
    > solve it?


    An explicit function template specialization is a definition of
    a function, not of a function template. Functions can only be
    defined once in a program, not once in every translation unit.

    Change the explicit specialization to a declaration (without
    a function body), and define it in one, and only one translation
    unit. (It the example, the function is so simple that you could
    probably inline it, but in general, this is something you would
    probably want to avoid.)

    --
    James Kanze
    James Kanze, Aug 17, 2010
    #4
    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. Jeff
    Replies:
    2
    Views:
    416
  2. Simon G Best
    Replies:
    2
    Views:
    555
    Simon G Best
    Dec 29, 2006
  3. Rahul
    Replies:
    8
    Views:
    389
    Lionel B
    Jul 16, 2007
  4. Replies:
    17
    Views:
    679
    Michael DOUBEZ
    Dec 23, 2008
  5. Igor R.
    Replies:
    4
    Views:
    480
    Igor R.
    Nov 11, 2009
Loading...

Share This Page