我写的第一个模板类

    很简单的一个列表类,而且并不是很完善,仅限于在自己的程序中使用。有很多很基本的东西还没搞懂。不过我一直奇怪的是:我将这个类的声明和实现放在不同的文件中时(.h, .cpp),最后在引用用这个类的地方都会出现连接错误,无奈只能将声明和实现放在同一个文件中了(.h),模板类的编译和连接机制到底是怎样的呢?
None.gif #pragma once
None.gif
None.giftemplate
< typename NodeType >
None.gif
class  TQueue;
None.gif
None.giftemplate 
< typename NodeType >
None.gif
class  QNode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    NodeType 
* node;
InBlock.gif    QNode
<NodeType> * nextQNode;
InBlock.gif    QNode
<NodeType> * prevQNode;
InBlock.gif    friend 
class TQueue<NodeType>;
ExpandedBlockEnd.gif}
;
None.gif
None.giftemplate 
< typename NodeType >
None.gif
class  TQueue  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
public:
InBlock.gif    TQueue(
void);
InBlock.gif    
~TQueue(void);
InBlock.gif
InBlock.gif    WORD GetCount();
InBlock.gif
InBlock.gif    
// Queue Behavior
InBlock.gif
    NodeType * PickHead();
InBlock.gif    
void AppendTail(NodeType * _newNode);
InBlock.gif    BOOL IsEmpty();
InBlock.gif
InBlock.gif    
// Sequence Visit
InBlock.gif
    NodeType * GetNextNode(HANDLE & hcur);
InBlock.gif    NodeType 
* PickCurrentNode(HANDLE & hcur);
InBlock.gif
InBlock.gif
private:
InBlock.gif    WORD _queueLen;
InBlock.gif    QNode
<NodeType> * _queueHead, * _queueTail;
ExpandedBlockEnd.gif}
;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// --- imp --- ///
None.gif template  < typename NodeType >
None.gifTQueue
< NodeType > ::TQueue( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    _queueLen 
= 0;
InBlock.gif    _queueHead 
= NULL;
InBlock.gif    _queueTail 
= NULL;
ExpandedBlockEnd.gif}

None.gif
None.giftemplate 
< typename NodeType >
None.gifTQueue
< NodeType > :: ~ TQueue( void )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    NodeType 
* tmpNode = NULL;
InBlock.gif    
while(_queueLen>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        tmpNode 
= PickHead();
InBlock.gif        delete tmpNode;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.giftemplate 
< typename NodeType >  
None.gifWORD TQueue 
< NodeType > ::GetCount()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return _queueLen;
ExpandedBlockEnd.gif}

None.gif
None.gif
//  Queue Behavior
None.gif
template  < typename NodeType >
None.gifNodeType 
*  TQueue < NodeType > ::PickHead()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(_queueLen == 0)return NULL;
InBlock.gif    assert(_queueHead 
!= NULL);
InBlock.gif
InBlock.gif    QNode
<NodeType> * tmpQNode = _queueHead;
InBlock.gif    NodeType 
* tmpNode = NULL;
InBlock.gif
InBlock.gif    
if(_queueHead->nextQNode != NULL) // has node lest
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        _queueHead
->nextQNode->prevQNode = NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else _queueTail = NULL; // no nodes left, so set _queueTail = NULL;
InBlock.gif
    _queueHead = _queueHead->nextQNode;
InBlock.gif
InBlock.gif    _queueLen
--;
InBlock.gif    
InBlock.gif    tmpNode 
= tmpQNode->node;
InBlock.gif    delete tmpQNode;
InBlock.gif    
return tmpNode;
ExpandedBlockEnd.gif}

None.gif
None.giftemplate 
< typename NodeType >
None.gif
void  TQueue < NodeType > ::AppendTail(NodeType  *  _newNode)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    QNode
<NodeType> * newQNode = new QNode<NodeType>;
InBlock.gif    newQNode
->node = _newNode;
InBlock.gif    newQNode
->nextQNode = NULL;
InBlock.gif    
if(_queueLen == 0// Empty Queue
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        newQNode
->prevQNode = NULL;
InBlock.gif        _queueHead 
= newQNode;
InBlock.gif        _queueTail 
= newQNode;
InBlock.gif        _queueLen 
++;
InBlock.gif        
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
// Queue not Empty
InBlock.gif
    assert(_queueTail != NULL);
InBlock.gif    _queueTail
->nextQNode = newQNode;
InBlock.gif    newQNode
->prevQNode = _queueTail;
InBlock.gif    _queueTail 
= newQNode;
InBlock.gif    _queueLen 
++;
ExpandedBlockEnd.gif}

None.gif
None.giftemplate 
< typename NodeType >
None.gifBOOL TQueue
< NodeType > ::IsEmpty()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return (_queueLen==0);
ExpandedBlockEnd.gif}

None.gif
None.gif
//  Sequence Visit
None.gif
template  < typename NodeType >
None.gifNodeType 
*  TQueue < NodeType > ::GetNextNode(HANDLE  &  hcur)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if (hcur == 0x0000)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        hcur 
= (HANDLE)_queueHead;
InBlock.gif        
if(_queueHead != NULL) return _queueHead->node;
InBlock.gif        
else return NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    QNode
<NodeType> * tmpQNode = (QNode<NodeType>*)hcur;
InBlock.gif    
if(tmpQNode->nextQNode == NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        hcur 
= 0x0000;
InBlock.gif        
return NULL;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        hcur 
= (HANDLE)tmpQNode->nextQNode;
InBlock.gif        
return (tmpQNode->nextQNode->node);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.giftemplate 
< typename NodeType >
None.gifNodeType 
*  TQueue < NodeType > ::PickCurrentNode(HANDLE  &  hcur)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    assert(hcur 
!= NULL);
InBlock.gif    assert(_queueLen 
> 0);
InBlock.gif
InBlock.gif    QNode
<NodeType> * tmpQNode = (QNode<NodeType>*)hcur;
InBlock.gif    NodeType 
* tmpNode = NULL;
InBlock.gif
InBlock.gif    
if (tmpQNode->prevQNode == NULL) // is the head
InBlock.gif
        _queueHead = tmpQNode->nextQNode;
InBlock.gif    
else
InBlock.gif        tmpQNode
->prevQNode->nextQNode = tmpQNode->nextQNode;
InBlock.gif
InBlock.gif    
if (tmpQNode->nextQNode == NULL) // is the tail
InBlock.gif
        _queueTail = tmpQNode->prevQNode;
InBlock.gif    
else
InBlock.gif        tmpQNode
->nextQNode->prevQNode = tmpQNode->prevQNode;
InBlock.gif
InBlock.gif    tmpNode 
= tmpQNode->node;
InBlock.gif    delete tmpQNode;
InBlock.gif    hcur 
= NULL;
InBlock.gif    
return tmpNode;
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xgchang/archive/2004/11/12/63139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值