模板类继承问题 [英] Template class inheritance problem

查看:64
本文介绍了模板类继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有一个模板类的问题,我试图继承

来自。

基类的代码是:


模板< class T>

类BaseClass {

public:

BaseClass();

~BaseClass();


UINT getSize();

BOOL存在(UINT pos);

VOID swap(UINT nFirst,UINT nSecond);

VOID删除(UINT pos);


受保护:

T * _head;

UINT _size;


虚拟T * getNode(UINT pos);

};


然后我继承了这个类:


class AnotherClass:public BaseClass< VFS> {

public:

AnotherClass(); //将_head和_size初始化为0

~AnotherClass(); //使用BaseNode删除所有节点

函数

//插入节点并设置

值的其他方法VFS结构中的成员

};


AnotherClass中的其他方法是例如添加一个新节点,设置

的值VFS结构中的成员等。


这些类在头文件中声明并在cpp文件中定义。

编译时出现问题,当我得到4个链接器错误的

类型:

3 x错误LNK2019:未解析的外部符号

1 x错误LNK2001:未解析的外部符号


如果有人知道问题是什么,我会很高兴。

Thansk提前。

Hi. I have a problem with a template class that I''m trying to inherit
from.
The code for the base class is:

template <class T>
class BaseClass {
public:
BaseClass();
~BaseClass();

UINT getSize();
BOOL exists(UINT pos);
VOID swap(UINT nFirst, UINT nSecond);
VOID remove(UINT pos);

protected:
T* _head;
UINT _size;

virtual T* getNode(UINT pos);
};

I then inherit this class:

class AnotherClass: public BaseClass<VFS> {
public:
AnotherClass(); // Initializes _head and _size to 0
~AnotherClass(); // Removes all nodes by using BaseNode''s remove
function
// Other methods that inserts nodes and sets the values of the
members in the VFS struct
};

Other methods in AnotherClass is for example adding a new node, setting
values of members in the VFS struct etc.

These classes are declared in a header file and defined in a cpp file.
The problem arises when compiling, when I get 4 linker errors of the
type:
3 x error LNK2019: unresolved external symbol
1 x error LNK2001: unresolved external symbol

If anyone knows what the problem could be I would be very pleased.
Thansk in advance.

推荐答案

好吧,把BaseClass成员的定义放回标题。


问候,

Ben
Well, put the definitions of the BaseClass members back to the header.

Regards,
Ben


哇,做到了!但是我希望将这些定义放在另一个

文件而不是标题中,只是为了通过在标题中使用

类的声明来使事情变得更清晰,然后定义在其他地方。

这可能吗?

Wow, that did it! But I would prefer to have the definitions in another
file than the header, just to make things look cleaner by just having
declarations of classes in the header, and then define them elsewhere.
Is that possible?


Murkland< mu ****** @ gmail.com>写道:
Murkland <mu******@gmail.com> wrote:
哇,做到了!但我更喜欢将标题中的定义放在另一个文件而不是标题中,只是为了让事情看起来更清晰,只需在标题中声明类,然后在其他地方定义它们。这可能吗?
Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?




如果您的编译器支持模板的''export''关键字,是的。

但我非常怀疑它是的,实际上答案是否定的。


hth

-

jb


(rot13中的回复地址,先解读)



If your compiler supports the ''export'' keyword for templates, yes.
But I highly doubt that it does, so practically, the answer is no.

hth
--
jb

(reply address in rot13, unscramble first)


这篇关于模板类继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆