模板专业化是否属于头文件或源文件? [英] Do template specialisations belong into the header or source file?

查看:67
本文介绍了模板专业化是否属于头文件或源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译过程中,我收到多重定义"错误,该错误指的是头文件中的模板特化.我需要把专业化放在源文件中吗?

During compilation I get a "multiple definition" error, which refers to a template specialisation in a header file. Do I need to put the specialisations into the source file?

推荐答案

如果您具有特定的功能,则可以将它们放在.cpp文件中,也可以将它们放在标题中.

If it is functions you have specialized, you can either put them in the .cpp file, or make them inline in the header.

就像James指出的那样,如果您不使函数内联,则仍然必须声明标头中的专业化.否则,编译器不知道必须在其他地方查找它们.

Like James points out, if you don't make the functions inline, you still have to declare the specializations in the header. Otherwise the compiler doesn't know it has to look for them elsewhere.

然后可以将实现(定义)放在.cpp文件中.就像其他功能一样.

You can then put the implementations (definitions) in a .cpp file. Just like with other functions.

这篇关于模板专业化是否属于头文件或源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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