8.1 .h .cpp分别写声明和实现
8.2 但是由于 类模板的成员函数运行阶段才去创建,导致包含.h头文件,不会创建函数的实现,无法解析外部命令
8.3 解决方案 保护 .cpp文件 (不推荐)
8.4 不要进行分文件编写,写到同一个文件中,进行声明和实现,后缀名改为.hpp
8.5 约定俗成的
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include "Modulator.hpp"
int main(){
Modulator<string, int> mod("渣渣猫", 25);
mod.show();
system("pause");
return 0;
}
Modulator.hpp
#pragma once
using namespace std;
template<class T1, class T2>
class Modulator {
public:
Modulator(T1 name, T2 age);
void show();
T1 m_name;
T2 m_age;
};
template<class T1, class T2>
Modulator<T1, T2>::Modulator(T1 name, T2 age) {
this->m_age = age;
this->m_name = name;
}
template<class T1, class T2>
void Modulator<T1, T2>::show() {
cout << "名字:" << this->m_name << " 年龄:" << this->m_age << endl;
}