In the example given in ยง14.4.1 "Accelerated C++, A. Koenig and B. E. Moo" I have problems if I implement the template specialization like it is presented in the book.
The minimum working example (top down) is:
g++ str.cpp main.cpp
main.cpp:
#include "str.hpp"
int main(int argc, char** argv)
{
Str n; //def
return 0;
}
str.hpp:
#ifndef GUARD_str_h
#define GUARD_str_h
#include <vector>
#include "ptr.hpp"
class Str {
public:
Str(): data(new std::vector<char>) { }
private:
Ptr< std::vector<char> > data;
};
#endif
str.cpp:
#include "str.hpp"
ptr.hpp:
#ifndef GUARD_ptr_h
#define GUARD_ptr_h
#include <vector>
template<class T> T* clone(const T* tp);
template<> std::vector<char>* clone(const
std::vector<char>* vp);
template <class T> class Ptr {
public:
Ptr(): refptr(new size_t(1)), p(0) { }
Ptr(T* t): refptr(new size_t(1)), p(t) { }
~Ptr();
private:
T* p;
size_t* refptr;
};
#include "ptr.cpp"
#endif
ptr.cpp:
template<class T>
Ptr<T>::~Ptr()
{
if (--*refptr == 0) {
delete refptr;
delete p;
}
}
template<class T>
T* clone(const T* tp)
{
return tp->clone();
}
template<>
std::vector<char>* clone(const std::vector<char>* vp)
{
return new std::vector<char>(*vp);
}
The problem is the last template specialization
template<> std::vector<char>*
It gives an
multiple definition of 'std::vector<char...>
error. It only works with
template<> inline std::vector<char>*
1) I do not fully understand why I need "inline".
2) Is this an error in the book? I tried to put this template specialization in the ptr.hpp file. Again, it only works with "inline".
Thanks to all who can put some light on this issue.