C++类模板分文件编写

一.使用常规分文件创建方法实现类模板

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

include文件下的person.h

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

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

person.cpp

#include "person.h"

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;
}

mian.cpp

#include<iostream>
#include "person.h"
using namespace std;

void test01()
{
    Person<string,int> P("Jie",18);
    P.showPerson();
}


int main()
{
    test01();
    return 0;
}

编译报错。

因为类模板成员函数一开始是不会创建的,
当你去包含.h的时候,相当于只是让编译器看到了person.h(声明代码)。编译器没有看到person.cpp中的实现构造函数和 void showPerson()的代码。所以链接阶段失败。

二.解决方法

1:直接包含.cpp源文件 
2.将声明和实现写到同一个文件中,并更改后缀名为.hpp,.hpp是约定的名称。

2.1 直接包含.cpp源文件

把 .h改为.cpp时,相当于让编译器看到实现的代码。

#include<iostream>
#include "person.cpp"
using namespace std;

void test01()
{
    Person<string,int> P("Jie",18);
    P.showPerson();
}


int main()
{
    test01();
    return 0;
}

此时编译器可以成功编译了。
但是一般不用这种方法,因为一般是不会直接导入cpp源码的。

2.2 将.h和.cpp中的声明和实现内容写到一起,将后缀名改为.hpp文件

include/person.hpp

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

// 声明
template<class T1,class T2>
class Person
{
public:
    Person(T1 name,T2 age);
    void showPerson();
    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;
}

mian.cpp

#include<iostream>
#include "person.hpp"
using namespace std;

void test01()
{
    Person<string,int> P("Jie",18);
    P.showPerson();
}


int main()
{
    test01();
    return 0;
}

编译:

g++  main.cpp -Iinclude

运行:

./a.out
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当你想要将模板函数的实现与声明离到不同的文件中时,可以使用模板的显式实例化来实现模板函数的文件编写。下面是一个示例,演示如何使用模板将选择排序算法的实现和声明离到不同的文件中: 在`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`源文件中,并在需要使用模板函数的地方进行显式实例化。这样,在编译和链接时,编译器可以找到模板函数的实现并生成对应类型的函数定义。 希望这个示例能满足你的需求!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

computer_vision_chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值