关于c++Template的问题

hanbaocheng 2009-05-23 10:51:10
----------------state.h--------------------
#ifndef STATE_H
#define STATE_H

#include"server.h"

template<class Type1,class Type2> class server;

template<class Type1,class Type2>
class state
{
public:
state(server<Type1,Type2> *){}
~state(){}
void init(){}
};

#endif
--------------server.h---------------------
#ifndef SERVER_H
#define SERVER_H

#include<string>
using std::string;
#include "state.h"
template<class Type1, class Type2> class state;

template<class Type1, class Type2>
class server:public Type1
{
friend class state<string,string>;
public:
server(Type2 *);
~server();
void init();
};

typedef server<string,string>
Service;
#endif
---------------main----------------------
#include<string>
using std::string;
#include "server.h"

void main()
{

string str="fsadfasdfsadfas";
Service * s=new Service(&str);
s->init();

}
------------------error------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall server<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::alloca
tor<char> > >::init(void)" (?init@?$server@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall server<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<c
har> > >::server<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<c
har>,class std::allocator<char> > *)" (??0?$server@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@@QAE@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Debug/template_test.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
-----------------------------------------

在VC6上编译通过,但是连接有错
怎么回事呢,模板高手指教
...全文
226 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jalien 2009-05-24
  • 打赏
  • 举报
回复
现在的编译器都可以将模板声明预定义放在不同文件中,没看到lz的实现,lz只要在你的#endif
前面加上#include"你的实现文件",然后同样对cpp实现文件使用包含警卫指令即可。这个叫包含编译,所有编译器都实现了包含编译。但不是所有的编译器都实现了分别编译模型,分别编译模型要使用export指令。[Quote=引用 2 楼 ilysony 的回复:]
现在暂时没有编译器能支持模板类的声明和定义在不同文件中
[/Quote]
lzh9955 2009-05-23
  • 打赏
  • 举报
回复
Service * s=new Service(&str);

blingpro 2009-05-23
  • 打赏
  • 举报
回复
实现与声明都放在头文件里,vc对模板不支持分离编译
Sou2012 2009-05-23
  • 打赏
  • 举报
回复
现在暂时没有编译器能支持模板类的声明和定义在不同文件中
baihacker 2009-05-23
  • 打赏
  • 举报
回复
你的实现放到哪里去了?
放到头文件里.

65,211

社区成员

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

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