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

mayulin110 2008-11-11 11:34:55
头文件定义部分:
#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)
...全文
100 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayulin110 2008-11-12
  • 打赏
  • 举报
回复
放到一个头文件中可以调试通过,但如何改才能使定义和实现分离,因为我打算在这个基础上在进行一些扩充
太乙 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mayulin110 的回复:]
除了那个头文件的问题,其他的都改了,单还是编译的时候没问题,连接的的时候报错那些错误!
感觉是实例化的时候出了问题,不知道问题在什么地方
[/Quote]



拿到一个.h文件里还报错?

就像我六楼代码一样?

我六楼代码可没错!

mayulin110 2008-11-12
  • 打赏
  • 举报
回复
除了那个头文件的问题,其他的都改了,单还是编译的时候没问题,连接的的时候报错那些错误!
感觉是实例化的时候出了问题,不知道问题在什么地方
太乙 2008-11-12
  • 打赏
  • 举报
回复



模板的定义和实现支持不好~~~

最好别分开!


.h:

#ifndef CLASSDEF
#define CLASSDEF
#include <iostream>
using namespace std;

template <class type> class SingleLink;

template <class T>
class Node
{
public:
friend class SingleLink <T>;
T data;
Node *Next;
};

template <class type>
class SingleLink
{
public:
SingleLink();
virtual ~SingleLink();

bool insert(type Element);//在当前位置插入值
private:
Node <type> *pHead,*pCurNode,*ptemp;
};
//#include"mycpp.cpp"
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;
pCurNode->Next = ptemp;
}
return true;
}

#endif




main.cpp

#include "head.h"

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;
}





太乙 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mayulin110 的回复:]

pHead = pCurNode = ptemp = new Node <type>; //这里写错了!!
要分配一个Node 类型的节点作为头节点,在这个地方怎么写?
[/Quote]


你以前写的是:

pHead = pCurNode = ptemp = new Node <Type>;



mayulin110 2008-11-12
  • 打赏
  • 举报
回复
谢谢
太乙 2008-11-12
  • 打赏
  • 举报
回复



帮你查了一下;

http://topic.csdn.net/u/20071026/15/fad0132c-692a-4c61-8e87-0bfabd0beba4.html

http://www.cnblogs.com/chio/archive/2007/09/08/886390.html




太乙 2008-11-12
  • 打赏
  • 举报
回复
我试了一下,不是很好使~~
太乙 2008-11-12
  • 打赏
  • 举报
回复
http://hi.baidu.com/beckbeckham/blog/item/24bff8454527d73886947317.html
mayulin110 2008-11-11
  • 打赏
  • 举报
回复
pHead = pCurNode = ptemp = new Node <type>; //这里写错了!!
要分配一个Node 类型的节点作为头节点,在这个地方怎么写?
如果把#include "SingleLink.cpp"放在头文件的末尾,编译的时候会报错
fatal error C1004: unexpected end of file found
mayulin110 2008-11-11
  • 打赏
  • 举报
回复

pHead = pCurNode = ptemp = new Node <type>; //这里写错了!!
要分配一个Node 类型的节点作为头节点,在这个地方怎么写?
太乙 2008-11-11
  • 打赏
  • 举报
回复

还有啊:

#include "SingleLink.cpp"

这不在.cpp里这么用

如果要实现模板类的定义和实现分离,可以在类定义的头文件末尾加上:

#include "SingleLink.cpp"

当然,这只是在vc6.0下能行,其他编译器,不敢说!






太乙 2008-11-11
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;

template <class type> class SingleLink;

template <class T>
class Node
{
public:
friend class SingleLink <T>;
T data;
Node *Next; //这里直接Node就行!
};

template <class type>
class SingleLink
{
public:
SingleLink();
virtual ~SingleLink();

bool insert(type Element);//在当前位置插入值
private:
Node <type> *pHead,*pCurNode,*ptemp;
};


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;
}

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;
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧