非模板类与模板类的多个定义 [英] Multiple definitions of a non-template class vs. a template class

查看:157
本文介绍了非模板类与模板类的多个定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编译器抱怨在多个.cpp文件中定义的非模板类,但是对于在各种.cpp文件(通过包含该类的.inl文件)中复制的模板类,即使该类在多个.cpp文件中显式实例化了吗?

解决方案

模板函数是内联的,在多个编译单元中定义,只要每个定义都相同即可。


Why do compilers complain about a non-template class defined in multiple .cpp files but are fine with a template class the definition of which is duplicated across various .cpp files (via inclusion of the .inl file of the class), even if the class is explicitly instantiated in even multiple .cpp files?

解决方案

Template functions are inline, and inline functions are allowed to be defined in multiple compilation units as long as each definition is identical.

这篇关于非模板类与模板类的多个定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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