header 个文件的重新定义错误 - cpp
Redefinition error with header files - cpp
我在命名空间下的 header 文件中定义了一个模板函数。当我将此 header 包含在同一项目的两个源文件中时。我没有收到重定义错误。
/* template.h */
namespace x
{
template<typename T>
function(t)
{
/* implementation */
}
}
/*test.cpp*/
#include "template.h"
/* test2.cpp */
#inlcude "template.h"
在上述情况下,我没有收到任何重定义错误。 .为什么我没有收到任何错误信息?
因为隐式模板实例化的行为就好像它们是隐式的 inline
:所有这些都在 link 时合并为一个。
在内部头文件中定义Header,当您将内部头文件包含到外部文件中时,所有头文件都将包含在内。
#ifndef FILE_H
#define FILE_H
/* ... Declarations etc here ... */
#endif
我在命名空间下的 header 文件中定义了一个模板函数。当我将此 header 包含在同一项目的两个源文件中时。我没有收到重定义错误。
/* template.h */
namespace x
{
template<typename T>
function(t)
{
/* implementation */
}
}
/*test.cpp*/
#include "template.h"
/* test2.cpp */
#inlcude "template.h"
在上述情况下,我没有收到任何重定义错误。 .为什么我没有收到任何错误信息?
因为隐式模板实例化的行为就好像它们是隐式的 inline
:所有这些都在 link 时合并为一个。
在内部头文件中定义Header,当您将内部头文件包含到外部文件中时,所有头文件都将包含在内。
#ifndef FILE_H
#define FILE_H
/* ... Declarations etc here ... */
#endif