第一次使用模板,出现了问题,请指点一下

mayulin110 mayulin110 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
头文件定义部分:
#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)
...全文
评论 13
收藏 已收藏
分享
返回顶部
mayulin110
C++ 语言 65,214 成员 250,514 内容