第一次使用模板,出现了问题,请指点一下
头文件定义部分:
#include <iostream>
using namespace std;
template <class type> class SingleLink;
template <class T>
class Node
{
public:
friend class SingleLink<T>;
T data;
class Node *Next;
};
template <class type>
class SingleLink
{
public:
SingleLink();
virtual ~SingleLink();
bool insert(type Element);//在当前位置插入值
private:
Node<type> *pHead,*pCurNode,*ptemp;
};
.cpp部分
#include "stdafx.h"
#include "SingleLink.h"
template <class type>
SingleLink<type>::SingleLink()
{//creat a head node
pHead = pCurNode = ptemp = new Node<Type>;
pHead->Next = NULL;
}
template <class type>
SingleLink<type>::~SingleLink()
{
pCurNode = pHead;
while(pCurNode)
{
pHead = pHead->Next;
delete pCurNode;
pCurNode = pHead;
}
}
template <class type>
bool SingleLink<type>::insert(type element)
{//insert the element into current position
if(!pCurNode)
return false;//no link because no head node
else// insert the element into current position
{
ptemp = pCurNode->Next;
pCurNode = pCurNode->Next = new Node<type>;
pCurNode->data = element->data;
pCurNode->Next = ptemp;
}
return true;
}
主程序部分
#include "stdafx.h"
//#include "SingleLink.h"
#include "SingleLink.cpp"
int main(int argc, char* argv[])
{
int a[5] = {1,2,3,4,5};
SingleLink<int> linklist;
for(int i = 0; i < 5; i++)
{
linklist.insert(a[i]);
}
return 0;
}
报的错误:
Compiling...
SLink.cpp
e:\workspace\slink\singlelink.cpp(17) : error C2143: syntax error : missing ';' before '<'
e:\workspace\slink\singlelink.cpp(17) : error C2501: 'SingleLink' : missing storage-class or type specifiers
e:\workspace\slink\singlelink.cpp(17) : error C2059: syntax error : ';'
e:\workspace\slink\singlelink.cpp(17) : error C2143: syntax error : missing ';' before '<'
e:\workspace\slink\singlelink.cpp(24) : error C2588: '::~SingleLink' : illegal global destructor
e:\workspace\slink\singlelink.cpp(24) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
SLink.obj - 6 error(s), 0 warning(s)
#include <iostream>
using namespace std;
template <class type> class SingleLink;
template <class T>
class Node
{
public:
friend class SingleLink<T>;
T data;
class Node *Next;
};
template <class type>
class SingleLink
{
public:
SingleLink();
virtual ~SingleLink();
bool insert(type Element);//在当前位置插入值
private:
Node<type> *pHead,*pCurNode,*ptemp;
};
.cpp部分
#include "stdafx.h"
#include "SingleLink.h"
template <class type>
SingleLink<type>::SingleLink()
{//creat a head node
pHead = pCurNode = ptemp = new Node<Type>;
pHead->Next = NULL;
}
template <class type>
SingleLink<type>::~SingleLink()
{
pCurNode = pHead;
while(pCurNode)
{
pHead = pHead->Next;
delete pCurNode;
pCurNode = pHead;
}
}
template <class type>
bool SingleLink<type>::insert(type element)
{//insert the element into current position
if(!pCurNode)
return false;//no link because no head node
else// insert the element into current position
{
ptemp = pCurNode->Next;
pCurNode = pCurNode->Next = new Node<type>;
pCurNode->data = element->data;
pCurNode->Next = ptemp;
}
return true;
}
主程序部分
#include "stdafx.h"
//#include "SingleLink.h"
#include "SingleLink.cpp"
int main(int argc, char* argv[])
{
int a[5] = {1,2,3,4,5};
SingleLink<int> linklist;
for(int i = 0; i < 5; i++)
{
linklist.insert(a[i]);
}
return 0;
}
报的错误:
Compiling...
SLink.cpp
e:\workspace\slink\singlelink.cpp(17) : error C2143: syntax error : missing ';' before '<'
e:\workspace\slink\singlelink.cpp(17) : error C2501: 'SingleLink' : missing storage-class or type specifiers
e:\workspace\slink\singlelink.cpp(17) : error C2059: syntax error : ';'
e:\workspace\slink\singlelink.cpp(17) : error C2143: syntax error : missing ';' before '<'
e:\workspace\slink\singlelink.cpp(24) : error C2588: '::~SingleLink' : illegal global destructor
e:\workspace\slink\singlelink.cpp(24) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
SLink.obj - 6 error(s), 0 warning(s)
...全文
相关推荐
魔方财务指点模板M1+M2全套源码
魔方财务指点模板M1+M2全套源码 M1和M2两套模板+后台可控插件,压缩包内包含M1+M2两套完整模板(用户中心+购物车+首页)以及后台可控插件,可以直接设置logo等信息,都是精心修复过的 使用教程: app和public文件夹上传到魔方财务根目录解压覆盖,install.sql导入到你的魔方财务数据库,后台启用模板即可!
成批请购单精品模板方案.doc
成批请购单精品模板方案.doc
解决OFFICE文件模板错误问题
对办公软件出现的安全模板问题 用以下方法 选择“文件夹选项,打开隐藏文件
请购物品登记表模板.doc
请购物品登记表模板.doc
请购物品登记单精品模板方案.doc
请购物品登记单精品模板方案.doc


