类模板分文件编写
//template.hpp
#include <iostream>
template<class typeName>
class Person
{
public:
Person(typeName name);
void show();
protected:
typeName m_name;
};
template<class typeName>
Person<typeName>::Person(typeName name):
m_name(name)
{
}
template<class typeName>
void Person<typeName>::show()
{
std::cout << "name " << m_name << std::endl;
}
//template_test.cpp
#include <iostream>
#include "template.hpp"
int main()
{
Person<std::string> p("furong");
p.show();
return 0;
}
$ g++ template*.cpp
$ ./a.out
name furong
主流的解决办法:将类模板成员函数写到一起,并将后缀名改为hpp