当前位置:网站首页>模板类的声明和定义
模板类的声明和定义
2022-07-17 04:59:00 【maplezys】
模板类的实现可以放到.cpp文件里吗?
先放结论:涉及到模板和模板特例化时,一般最好将模板和模板特例化一起放在对应的.h文件中。
以下面的代码为例:
//---test.h---//
template<typename T>
class A {
public:
void f(); //这里只是声明
};
//---test.cpp---//
#include "test.h"
template<typename T>
void A<T>::f() {
// do somethong...
}
//---main.cpp---//
#include "test.h"
int main(){
A<int> a;
a.f();
}
在main函数中,编译器并不知道A::f的定义,因为它不再test.h文件里面,于是编译器只好寄希望于连接器,希望它能够在其他的.obj里面找到A::f的实现体,在上述的例子中就是test.obj。然而,test.obj中是没有A::f的二进制代码的。因为C++明确表示当一个模板不被用到的时候它就不该被具现出来,而此处的test.cpp是没有用到A::f的,于是在test.obj中是没有关于它的二进制代码的,于是连接器便会给出连接错误。但是,如果在test.cpp中写一个函数,其中使用到了A::f,则编译器便会将其具现出来,即使main函数中没有用到。
解决方法:
- 在实例化要素中让编译器看到模板定义
- 用另外的文件来显式地实例化类型,这样连接器就能看到该类型
- 使用export关键字(大部分编译不支持)
第一种方法就是将模板的声明和定义全卸载.h文件中,这样编译器就能直接编译main.cpp。第二种便是新建一个文件,或者就在test.cpp中,显式地实例化得到类型,这样便会被连接器找到相对饮管道二进制代码。
边栏推荐
- Extreme video compression using the pellet toolbox
- VB.NET插件开发- 提取文件
- 面临的挑战和优势,并预测NeRF最终将取代Deepfake
- 数据库学习笔记(一)检索数据
- Database and the future of open source
- Summary of black screen problems in unity UMP packaging
- PY3 redis general call
- [TA frost wolf \u may - hundred people plan] Figure 2.5 bump mapping
- Unity UMP打包黑屏问题总结
- [Lipschitz] simulation of Lipschitz Lipschitz exponent based on MATLAB
猜你喜欢
随机推荐
Tasking 新建 AURIX TC37X demo 工程
minio安装部署及使用
Redis 集群面试题
PHP array_ Filter empty leave 0, false
TCP/IP 协议
[Unity] 交互之双击
tidb 学习
Fudan micro fmql (domestic zynq) [PS of IAR bare metal development] - non byte aligned access
CKS 核心知识点概述
JS中Class类的静态属性和静态方法
Golang reverse slice code example
UE-插件 ElectronicNodes 5.0.0/4.23-4.27
Project team summer vacation summary 02
Common go file writing methods
New network security architecture: Zero trust security
And predicts that nerf will eventually replace deepfake
谷歌浏览器截全图小技巧
Emqx pressure test tread pit for your reference
Autojs learning - realize blissful pure land
Sphinx遇到的问题









