[问]从模板中派生 [英] [Q] Deriving from templates

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

问题描述

我有一个相当简单的设置,我认为可以工作,但我是

得到链接错误。

模板< class T>

class TType

{

public:

TType(void){}

virtual~ TType(void){}

virtual void Release(void){}

virtual void Retain(void){}

protected:

T mRef;

私人:

};

模板< class T>

class TTree:

public TType< T>

{

public:

TTree(void){}

虚拟~TTree(无效){}

受保护:

私人:

};

class CTree:

public TTree< long>

{

public:

CTree(void ){}

virtual~CTree(void){}

布尔值Create(void){Release();返回false; }

受保护:

私人:

};

在其他地方,我说:


CTree myTree;


所有内容都会编译,但我得到的链接错误声称来自TType类的所有内容都是未定义的。 />

有什么想法吗?



解决方案

Eric写道:

我有一个相当简单的设置,我认为可行,但我收到链接错误。


您是否尝试过常见问题解答?

[..]



Victor Bazarov< v。******** @ comAcast.net>写道:

Eric写道:

我有一个相当简单的设置,我认为可行,但我收到链接错误。



您是否尝试过常见问题解答?




是的。不幸的是,它没有多大帮助。我很难点击

我的高跟鞋两次并想到堪萨斯 - 在这种情况下,至少34.13似乎是

最适用的部分。


当然,也可能没有帮助的是我发布的内容将会编译和链接 - 尝试一个我忽略首先测试的捷径。


因此,需要将其更改为:


(此代码再次告诉我TType中的所有内容都未定义。)


// TType.h

// TType.h

// TType.h


emplate < class T>

class TType

{

public:

TType(void);

virtual~TType(void);

virtual void Release(void);

virtual void Retain(void);

保护:

T mRef;

私人:

};


// TType.cp

// TType.cp

// TType.cp


模板< class T>

TType< T>: :

TType(无效)

{

mRef = NULL;

}

模板< class T>

TType< T> ::

~TType(无效)

{

}

模板< class T>

void

TType< T> ::

发布(无效) )

{

}

模板< class T>

无效

TType< T> ::

保留(无效)

{

}


// TTree.h

// TTree.h

// TTree.h


模板< class T>

class TTree:

public TType< T>

{

public:

TTree( void){}

virtual~TTree(void){}

protected:

private:

};

class CTree:

public TTree< long>

{

public:

CTree(void){}

virtual~CTree(void){}

布尔值Create(void){Release();返回false; }

受保护:

私人:

};




Eric写道:

Victor Bazarov< v。******** @ comAcast.net>写道:

Eric写道:

我有一个相当简单的设置,我认为可行,但我我是否收到链接错误。
您是否尝试过常见问题解答?



是的。不幸的是,它没有多大帮助。我很难点击
我的高跟鞋两次并想到堪萨斯州 - 在这种情况下,至少34.13似乎是最适用的部分。




不,34.14确实。

当然,也可能没有帮助的是我发布的内容将编译和链接 - 尝试了一个我忽略首先测试的捷径。 br />
所以,需要改为:



[...]


I''ve got a fairly simple setup that I thought would work, but I am
getting link errors.
template <class T>
class TType
{
public:
TType( void ) {}
virtual ~TType( void ) {}
virtual void Release( void ) {}
virtual void Retain( void ) {}
protected:
T mRef;
private:
};
template <class T>
class TTree :
public TType<T>
{
public:
TTree( void ) {}
virtual ~TTree( void ) {}
protected:
private:
};
class CTree :
public TTree<long>
{
public:
CTree( void ) {}
virtual ~CTree( void ) {}
Boolean Create( void ) { Release(); return false; }
protected:
private:
};
Elsewhere, I say:

CTree myTree;

Everything compiles, but the link errors I get claim that everything
from the TType class is undefined.

Any ideas?




解决方案

Eric wrote:

I''ve got a fairly simple setup that I thought would work, but I am
getting link errors.
Have you tried the FAQ?
[..]



Victor Bazarov <v.********@comAcast.net> wrote:

Eric wrote:

I''ve got a fairly simple setup that I thought would work, but I am
getting link errors.



Have you tried the FAQ?



Yes. Unfortunately, it isn''t helping much. I have a hard time clicking
my heels twice and thinking of Kansas - at least 34.13 seems to be the
most applicable section in this case.

Of course, what also may not be helping is that what I posted will
compile and link - attempted a short cut that I neglected to test first.

So, it needs to be changed to:

(This code, again, tells me that everything in TType is not defined.)

// TType.h
// TType.h
// TType.h

emplate <class T>
class TType
{
public:
TType( void );
virtual ~TType( void );
virtual void Release( void );
virtual void Retain( void );
protected:
T mRef;
private:
};

// TType.cp
// TType.cp
// TType.cp

template <class T>
TType<T>::
TType( void )
{
mRef = NULL;
}
template <class T>
TType<T>::
~TType( void )
{
}
template <class T>
void
TType<T>::
Release( void )
{
}
template <class T>
void
TType<T>::
Retain( void )
{
}

// TTree.h
// TTree.h
// TTree.h

template <class T>
class TTree :
public TType<T>
{
public:
TTree( void ) {}
virtual ~TTree( void ) {}
protected:
private:
};
class CTree :
public TTree<long>
{
public:
CTree( void ) {}
virtual ~CTree( void ) {}
Boolean Create( void ) { Release(); return false; }
protected:
private:
};




Eric wrote:

Victor Bazarov <v.********@comAcast.net> wrote:

Eric wrote:

I''ve got a fairly simple setup that I thought would work, but I am
getting link errors.
Have you tried the FAQ?


Yes. Unfortunately, it isn''t helping much. I have a hard time clicking
my heels twice and thinking of Kansas - at least 34.13 seems to be the
most applicable section in this case.



No, 34.14 does.

Of course, what also may not be helping is that what I posted will
compile and link - attempted a short cut that I neglected to test first.

So, it needs to be changed to:


[...]


这篇关于[问]从模板中派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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