template specialisation

Aug 16, 2012 at 5:57pm
Hi,
I am trying to specialise a template for the boolean type, but what I'm stuck with the multiple definition error:

/home/atari/workspace/eclipse/cpp_epdemo2/Debug/../common.h:19: multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> > my::str<bool>(bool const&)'
./common.o:/home/atari/workspace/eclipse/cpp_epdemo2/Debug/../common.h:19: first defined here
./main.o: In function `std::basic_string<char, std::char_traits<char>, std::allocator<char> > my::str<bool>(bool const&)':
/home/atari/workspace/eclipse/cpp_epdemo2/Debug/../common.h:19: multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> > my::str<bool>(bool const&)'
./common.o:/home/atari/workspace/eclipse/cpp_epdemo2/Debug/../common.h:19: first defined here


my code is:
1
2
3
4
5
6
7
8
9
10
11
// attempts to convert an object\integer in a string
template <typename T>
std::string str(const T& value){
	std::stringstream stream;
	stream << value;
	return stream.str();
}
template <>
std::string str<bool>(const bool& value){
	return (value ? "true" : "false");
}


what am I missing?

regards
-dean
Aug 16, 2012 at 6:42pm
I think if you have the member function template specializations in the same header file as the base template function you will get multiple definition errors.
Aug 16, 2012 at 6:49pm
so what's the proper way to obtain the same functionality?

regards
- dean
Aug 19, 2012 at 2:06pm
no ideas ?:)
Aug 19, 2012 at 2:12pm
declare str<bool> in the header and define it in a source file.
Aug 20, 2012 at 10:55am
thanks Peter!
Topic archived. No new replies allowed.