Template的是实现需要放在头文件中

2 views
Skip to first unread message

邱戈川

unread,
Feb 27, 2006, 10:22:16 PM2/27/06
to 基于ACE和SpiderMonkey的SMS虚拟运营系统
今天写Template的时候发现LNK2001错误,说找不到Template类的构造函数和析构函数。找MSDN发现:
The definition of member template is outside the class. Visual C++ has
a limitation in which member templates must be fully defined within the
enclosing class. See KB article Q239436 for more information about
LNK2001 and member templates.

只好将实现放入头文件了。

如果Template类需要继承:可以这样写:

template <class T> class ChildClass : public ParentClass<T>
{
......
};

JackyYang

unread,
Mar 1, 2006, 9:45:57 AM3/1/06
to 基于ACE和SpiderMonkey的SMS虚拟运营系统
对。另补充一点原因:
C++语言的template机制目前尚未形成统一的标准,不同的C++编译程序有不同的实现。Microsoft和Borland等都不允许将类界面与类实现分开,即需要将类声明与实现放在同一个文件(一般是.h)。编译程序并不为Template类的定义生成任何代码,仅当Template实例化时才产生真正的代码。由于未实例化的template定义没有加入目标文件(.obj)中,当我们尝试在.lib中包含Template类时就会出现链接错误(vc++中报LNK2001)。

candid Qiu

unread,
Mar 1, 2006, 9:52:34 AM3/1/06
to ACE...@googlegroups.com
果然,在obj目录找不到相应的.obj文件。
Reply all
Reply to author
Forward
0 new messages