首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

类模板的有关问题-请问

2012-03-21 
类模板的问题--------------请教类模板如下://#includestdafx.h #include iostreamusingnamespacestd

类模板的问题--------------请教
类模板如下:

//

#include   "stdafx.h "
#include <iostream>
using   namespace   std;

template <class   Type>

class   Node
{
public:
Type   data;//链表数据域

Node(const   Type&   item,Node <Type> *   ptrnext   =   NULL);

void   InsertAfter(Node <Type> *   p);

Node <Type> *   DeleteAfter(void);

Node <Type> *   NextNode(void)   const;
private:
//next   为指向下一节点的指针
Node <Type>   *next;
};

//构造函数初始化
Node <Type> ::Node(const   Type   &item,Node <Type> *   ptrnext   =   NULL):data(item),next(ptrnext)
{

}

//表操作函数,返回一个next值,指向下一个节点的值
Node <Type> *   Node <Type> ::NextNode(void)   const
{
return   next;
}


int   _tmain(int   argc,   _TCHAR*   argv[])
{
//Node <int>   obj;

//***************************************
system( "PAUSE ");
return   0;
}

错误如下:

d:\project\listcreat\listcreat.cpp(24):   error   C2065:   “Type”   :   未声明的标识符

d:\project\listcreat\listcreat.cpp(24):   error   C2955:   “Node”   :   使用类模板需要模板参数列表     d:\project\listcreat\listcreat.cpp(21)   :   参见“Node”的声明

d:\project\listcreat\listcreat.cpp(24):   error   C2955:   “Node”   :   使用类模板需要模板参数列表
                d:\project\listcreat\listcreat.cpp(21)   :   参见“Node”的声明

d:\project\listcreat\listcreat.cpp(24):   error   C2143:   语法错误   :   缺少“,”(在“&”的前面)

d:\project\listcreat\listcreat.cpp(24):   error   C2511:   “Node::Node(const   int)”   :   “Node”中没有找到重载的成员函数

d:\project\listcreat\listcreat.cpp(44):   fatal   error   C1004:   遇到意外的文件结束

不知道错误在哪里,请指点!


[解决办法]
template <class Type>
Node <Type> * Node <Type> ::NextNode(void) const
把C++语法书放在随手可及的地方吧。
[解决办法]
另外,默认参数只在类定义中一处加.
函数定义时不加.
[解决办法]
template <class Type>
class Node
{
public:
Type data;
//////////////
Node(const Type& item,Node <Type> * ptrnext = NULL)
{ }
/////////////
void InsertAfter(Node <Type> * p);
Node <Type> * DeleteAfter(void);
Node <Type> * NextNode(void) const;
private:
Node <Type> *next;
};

//////////////////////////////////////////
template <class Type>
/////////////////////////////////////////
Node <Type> * Node <Type> ::NextNode(void) const
{
return next;
}

热点排行