类模板分文件编写
作者:互联网
//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
标签:文件,name,show,hpp,Person,template,编写,typeName,模板 来源: https://www.cnblogs.com/zhangxuechao/p/16504298.html