C++链表的使用问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 03:28:17
我编了一个最大公倍数的程序,并使用链表,因为不熟悉,老是报错,程序如下:
.h文件
template<class T>class beishu;
template<class T>
class Node
{
friend int main();
friend beishu<T>;
private:
T data;
Node<T> *link;
};
template<class T>
class beishu
{
friend int main();
public:
beishu(){first=0;}
virtual ~beishu();
bool IsEmpty()const{return first==0;}
private:
Node<T> *first;
};
template<class T>
beishu<T>::~beishu()
{
Node<T> *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}

#endif // !defined(AFX_BEISHU_H__31B5C28D_8002_4BA8_A2BC_95481FC8788D__INCLUDED_)

.cpp文件
#include "beishu.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
/////////////////////////

friend int main()//这个友元声明也太……

建议是:先不要用模板来写,C++的模板有一些特性新手很容易出错;然后关于最大公倍数的问题可以网上搜一下欧几里德的GCD算法