template代码在头文件和源文件的划分问题

wangshuaijie 2008-11-13 05:08:47
我写了一个程序测试c++中template的inclusion compilation model,我用的编译器是VS 2008 exrpess中的VC++,我的计划是将template的声明放在头文件templateFunction.h里,而template的定义放在源文件templateFunction.cpp里,我的代码如下:
templateFunction.h:
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
template<typename T>
class A
{
public:
void test(const T&);
};
#include "templateClass.cpp"
#endif

templateFunction.cpp:
#include "templateFunction.h"
template<typename T> int compare(const T& v1,const T& v2)
{
if(v1<v2) return -1;
if(v2<v1) return 1;
return 0;
}

但是这样编译器报错,错误信息是:
1>------ Build started: Project: templateFunctionTest1, Configuration: Debug Win32 ------
1>Compiling...
1>templateFunction.cpp
1>e:\c++workspace\templatefunctiontest1\templatefunctiontest1\templatefunction.cpp(9) : error C2995: 'int compare(const T &,const T &)' : function template has already been defined
1> e:\c++workspace\templatefunctiontest1\templatefunctiontest1\templatefunction.h(3) : see declaration of 'compare'
1>Generating Code...
1>Skipping... (no relevant changes detected)
1>testMain.cpp
1>Build log was saved at "file://e:\c++workspace\templateFunctionTest1\templateFunctionTest1\Debug\BuildLog.htm"
1>templateFunctionTest1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

编译器认为compare这个template重复定义了,于是我在templateFunction.cpp中加上了header guard,这样templateFunction.cpp的代码变成了:
#ifndef TEMPLATEFUNCTION_CPP
#define TEMPLATEFUNCTION_CPP
#include "templateFunction.h"
template<typename T> int compare(const T& v1,const T& v2)
{
if(v1<v2) return -1;
if(v2<v1) return 1;
return 0;
}
#endif

这样代码编译就通过了,我的问题就是请问,为什么不在templateFunction.cpp中加header guard就报重复定义呢?
...全文
360 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallening 2008-11-13
  • 打赏
  • 举报
回复
templateFunction.cpp中去掉

#include "templateFunction.h"

别的不需要改变

另外:建议templateFunction.cpp改名为templateFunction.tcc
小人物- 2008-11-13
  • 打赏
  • 举报
回复
mark
liumingrong 2008-11-13
  • 打赏
  • 举报
回复
因为你在头文件中使用了#include "templateClass.cpp"
.cpp文件又包含.h
在编译.cpp文件时,先将.h包含进来,又发现了#include "templateClass.cpp",就重复包含了,除非加header guard
模板的分离定义还是不要的好
taodm 2008-11-13
  • 打赏
  • 举报
回复
#include "templateClass.cpp"
你就以为它是cpp文件啦?
可以这么说,#include的都是头文件。

wangshuaijie 2008-11-13
  • 打赏
  • 举报
回复
我先说说我的看法,头文件的定义是:
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
template<typename T>
class A
{
public:
void test(const T&);
};
#include "templateClass.cpp"
#endif

然后,我把源文件展开到头文件里,这样头文件就变成了:
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
template<typename T>
class A
{
public:
void test(const T&);
};



#include "templateFunction.h"
template<typename T> int compare(const T& v1,const T& v2)
{
if(v1<v2) return -1;
if(v2<v1) return 1;
return 0;
}



#endif

这里面又有一个#include "templateFunction.h",然后接着展开:
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
template<typename T>
class A
{
public:
void test(const T&);
};



#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
template<typename T>
class A
{
public:
void test(const T&);
};
#include "templateClass.cpp"
#endif


template<typename T> int compare(const T& v1,const T& v2)
{
if(v1<v2) return -1;
if(v2<v1) return 1;
return 0;
}



#endif

好了,到了这一步,由于一开始就有header guard了,所以红色的部分应该不参与编译,

这样看来是不应该出现重复定义的问题啊?为什么编译器报错呢?为什么我在

templateFunction.cpp加上了header guard就可以了呢?请各位高手指点

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧