C++ 类模板分文件编写

类模板分文件编写:

类模板成员函数 分文件编写 产生的问题以及解决方式是什么呢?


问题描述

类模板中**成员函数**创建时机是在**调用**阶段,导致分文件编写时**链接**不到

解决方案:

1、直接包含.cpp源文件:

在这里插入图片描述
person.h中的代码:

#pragma once
#include <iostream>
using namespace std;

template<class T1, class T2>
class Person {
public:
	Person(T1 name, T2 age);
	void showPerson();
public:
	T1 m_Name;
	T2 m_Age;
};

person.cpp中的主要代码:

//构造函数 类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {
	this->m_Name = name;
	this->m_Age = age;
}

//成员函数 类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson() {
	cout << "Name: " << this->m_Name << " Age:" << this->m_Age << endl;
}

main.cpp中的代码:

#include <iostream>
#include "person.hpp"
#include <windows.h>//用于函数SetConsoleOutputCP(65001);更改cmd编码为utf8
using namespace std;


void test01(){
    Person<string, int> p("tom", 20);
    p.showPerson();
}

int main(){

    SetConsoleOutputCP(65001);

    test01();

    system("pause");

    return 0;
}

因为成员函数的编译是在调用阶段,如果成员函数在.h中实现,这个实现并没有编译,所以会出现报错。那么,成员函数得单独在一个cpp文件中实现,这样比较麻烦。因此推荐第二种写法:

2、将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制 (推荐这种方式):

在这里插入图片描述

person.hpp中代码:

#pragma once  //防止头文件重复包含
#include <iostream>
using namespace std;
#include <string>

template<class T1, class T2>
class Person {
public:
	Person(T1 name, T2 age);
	void showPerson();
public:
	T1 m_Name;
	T2 m_Age;
};

//构造函数 类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {
	this->m_Name = name;
	this->m_Age = age;
}

//成员函数 类外实现
template<class T1, class T2>
void Person<T1, T2>::showPerson() {
	cout << "姓名: " << this->m_Name << " 年龄:" << this->m_Age << endl;
}

类模板分文件编写.cpp中代码:

#include<iostream>
using namespace std;

//#include "person.h"
#include "person.cpp" //解决方式1,包含cpp源文件

//解决方式2,将声明和实现写到一起,文件后缀名改为.hpp
#include "person.hpp"
void test01()
{
	Person<string, int> p("Tom", 10);
	p.showPerson();
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:主流的解决方式是第二种,将类模板成员函数写到一起,并将后缀名改为.hpp

附:C++函数分文件编写(VScode2021配置教程)

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当你想要将模板函数的实现与声明分离到不同的文件中时,可以使用模板的显式实例化来实现模板函数的分文件编写。下面是一个示例,演示如何使用模板将选择排序算法的实现和声明分离到不同的文件中: 在`selectionsort.h`头文件中,声明选择排序算法的模板函数: ```cpp #ifndef SELECTIONSORT_H #define SELECTIONSORT_H #include <vector> template<typename T> void selectionSort(std::vector<T>& arr); #include "selectionsort.cpp" // 包含模板函数的实现 #endif ``` 在`selectionsort.cpp`源文件中,实现选择排序算法的模板函数: ```cpp #include "selectionsort.h" template<typename T> void selectionSort(std::vector<T>& arr) { int n = arr.size(); for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } std::swap(arr[i], arr[minIndex]); } } // 显式实例化模板函数 template void selectionSort<int>(std::vector<int>& arr); template void selectionSort<double>(std::vector<double>& arr); // 可以根据需要添加其他类型的实例化 ``` 在主程序文件`main.cpp`中,使用选择排序算法进行排序: ```cpp #include <iostream> #include <vector> #include "selectionsort.h" int main() { std::vector<int> nums = {5, 2, 8, 9, 1}; selectionSort(nums); std::cout << "排序结果: "; for (const auto& num : nums) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` 在上面的示例中,`selectionsort.h`头文件中声明了选择排序算法的模板函数,并包含了`selectionsort.cpp`源文件,其中包含了模板函数的实现。 在`selectionsort.cpp`源文件中,我们使用了显式实例化来实例化选择排序算法的模板函数,以便在链接时能够生成对应类型的函数定义。这里示范了对`int`和`double`类型的实例化,你可以根据需要添加其他类型的实例化。 在主程序文件`main.cpp`中,我们包含了`selectionsort.h`头文件,并使用选择排序算法对向量进行排序。 请注意,在使用分文件编写模板时,需要将模板函数的实现放在`selectionsort.cpp`源文件中,并在需要使用模板函数的地方进行显式实例化。这样,在编译和链接时,编译器可以找到模板函数的实现并生成对应类型的函数定义。 希望这个示例能满足你的需求!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值