I have a container object that is templatized. I am trying to make a specialized constructor for float versions. Problem is, when the compiler attempts to compile the second object that uses the float version, I get a multiple definition of error.
NOTES: The entire class in in the h file. The file is wrapped with a definition (#ifndef, #define, and #endif). g++ version 3.4.6. This compiles fine with other compilers, e.g. Intel’s icc.
Code is similar to the following:
template <typename T>
class Container {
public:
Container();
virtual ~Container() {}
private:
std::vector<T> data;
// other members
};
template <> Container<float>::Container() {
// do something special
}
template <typename T> Container<T>::Container() {
// do default initialization
}
Any ideas? Thanks!
EDIT The objects being compiled are also going into separate shared objects, not sure if that has something to do with it.
Specializations still must abide by the one-definition rule just like any other non-template method. Either mark it inline or define the method body in a source file (not your header).