[在线等]VC9.0 模板类编译连接的问题,谢谢

zzwang 2008-10-26 10:20:48
只有3个文件 :

//root.h
#pragma once

#include <string>
#include <iostream>

template <typename T>
class root
{
public:
void show();
T t;
};

#include "root.cpp"


//root.cpp
#include "stdafx.h"

#include "root.h"

template <typename T>
void root<T>::show()
{
std::cout<<"root show"<<std::endl;
}


//main.cpp 在main.cpp中使用类root。
#include "stdafx.h"
#include "root.h"

int _tmain(int argc, _TCHAR* argv[])
{
root<int> r;
r.show();

return 0;
}


编译失败:
error C2995: “void root<T>::show(void)”: 函数模板已经定义

求成功编译连接的方法,谢谢!
...全文
128 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2008-10-27
  • 打赏
  • 举报
回复
因为一般的事实标准是模板的声明和定义不能分离.

因为在实例化点需要看到模板的定义.

export模型另计.
figo_tang 2008-10-26
  • 打赏
  • 举报
回复
root.h

#ifndef ROOT_H_
#define ROOT_H_

#include <string>
#include <iostream>

template <typename T>
class root
{
public:
void show();
T t;
};
#endif

root.cpp

#include "root.h"

template <typename T>
void root<T>::show()
{
std::cout << "root show" << std::endl;
}
int main()
{
root<int> r;
r.show();
return 0;
}

这样就可以编译通过,并且运行出结果,大家解释下
迷途的书童 2008-10-26
  • 打赏
  • 举报
回复
把root.h里的 #include "root.cpp"去掉。

因为编译root.h,root.cpp两文件都被编译目标文件,这时候show就都在两目标文件里啦。造成冲突!
baihacker 2008-10-26
  • 打赏
  • 举报
回复
可以用包含模型,导出模型,详细的见c++ templates
帅得不敢出门 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zzwang 的回复:]
引用 1 楼 baihacker 的回复:
模板的声明与定义怎么分开了...
应该放在头文件里的.
谢谢!我知道要放在一起,全部放在头文件里当然可以,但不能像非模板类一
样分开放么?
[/Quote]
大多数编译器不支持分离模式,目前你还是乖乖得放在一个头文件中吧.
sys0003 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zzwang 的回复:]
引用 5 楼 sys0003 的回复:
模板不支持分开编译,都要放到.h中,可以使用内联函数
C/C++ code#pragmaonce#include <string>#include <iostream>template <typename T>classroot
{public:voidshow();
T t;
};

template <typename T>inlinevoidroot <T>::show()
{
std::cout < <"root show" < <std::endl;
}


我在 root.h 中 #include "root.cpp" 也不行么?
[/Quote]

模板在编译时是把代码按模板定义替换为代码,所以模板类的成员函数都是放在类里或是内联函数的形式错在的。
你可以看看STL和ATL的模板实现。
机智的呆呆 2008-10-26
  • 打赏
  • 举报
回复

#pragma once

#include <string>
#include <iostream>

template <typename T>
class root
{
public:
void show();
T t;
};

//#include "root.cpp"




#include "stdafx.h"
#include "root.cpp"

int _tmain(int argc, _TCHAR* argv[])
{
root<int> r;
r.show();

return 0;
}



楼主还是 把模板的声明和定义写在一个文件里吧~~vc又不支持export
zzwang 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sys0003 的回复:]
模板不支持分开编译,都要放到.h中,可以使用内联函数
C/C++ code#pragmaonce#include<string>#include<iostream>template<typename T>classroot
{public:voidshow();
T t;
};

template<typename T>inlinevoidroot<T>::show()
{
std::cout<<"root show"<<std::endl;
}
[/Quote]

我在 root.h 中 #include "root.cpp" 也不行么?
zzwang 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Demon__Hunter 的回复:]
#include "root.cpp"试试~~~

[/Quote]
在哪里写 #include "root.cpp" ,我 root.h 中已经写过了!
sys0003 2008-10-26
  • 打赏
  • 举报
回复
模板不支持分开编译,都要放到.h中,可以使用内联函数

#pragma once

#include <string>
#include <iostream>

template <typename T>
class root
{
public:
void show();
T t;
};

template <typename T>
inline void root<T>::show()
{
std::cout<<"root show"<<std::endl;
}


zzwang 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
模板的声明与定义怎么分开了...
应该放在头文件里的.
[/Quote]

谢谢!我知道要放在一起,全部放在头文件里当然可以,但不能像非模板类一
样分开放么?
机智的呆呆 2008-10-26
  • 打赏
  • 举报
回复
#include "root.cpp"试试~~~
jieao111 2008-10-26
  • 打赏
  • 举报
回复
看来书再来问
baihacker 2008-10-26
  • 打赏
  • 举报
回复
模板的声明与定义怎么分开了...
应该放在头文件里的.

65,211

社区成员

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

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