可以在两个.cpp文件中模板功能吗? [英] Can template function in two .cpp files?

查看:83
本文介绍了可以在两个.cpp文件中模板功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我编写一个普通的模板函数,定义它并在两个不同的.cpp文件中声明它。如果它可以工作,我不会在定义的.cpp文件中使用它吗?



fiel1.cpp ////在此.cpp中定义它

模板< typename any =>

void fun(... )

{









}



file2.cpp ///使用它



模板< typenae any = >

虚假乐趣(...);



int main()

{< br $> b $ b。

fun(...);





}

If I Write a normal template function , define it and declare it in two differernt .cpp files.And If it will work ,I use it not in the defined .cpp files?

fiel1.cpp ////define it in this .cpp
template<typename any="">
void fun(...)
{
.
.
.
.
}

file2.cpp ///use it

template<typenae any="">
void fun(...);

int main()
{
.
fun(...);
.
.
}

推荐答案

如果要在多个源文件中使用它们,请在头文件(.h或.hpp)中声明模板。



模板是内联编译的,因此在实例化之前,必须在源代码中的某处声明和实现它们。
Declare your templates in a header file (.h or .hpp), if you want to use them in multiple source files.

Templates are compiled inline, so you must have them declared and implemented somewhere in the source, before they are instantiated.


这篇关于可以在两个.cpp文件中模板功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆