specializing a template function in a class

J

John

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_;
 
A

aryan

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; }
 
J

Jonathan Lee

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
 
J

James Kanze

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
/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.)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top