不知道錯的原因,希望大神幫忙解決,不勝感激。
#include<iostream>
using namespace std;
template <class T>
class List
{
public:
List(T *elem);
void Traverse();
private:
typedef struct LNode
{
T *m_elem;
struct LNode *next;
}Node,*LinkNode;
LinkNode head;
};
template <class T>
List<T>::List(T &elem)
{
head=new Node;
*(head->m_elem)=elem;
head->next=NULL;
}
int main(void)
{
double a=2.0;
List<double>head(a);
getchar();
return 0;
}
#include<iostream>
using namespace std;
template <class T>
class List
{
public:
List(T &elem);//修改,要和後面定義一致
~List();
void Traverse();
private:
typedef struct LNode
{
T *m_elem;
struct LNode *next;
}Node,*LinkNode;
LinkNode head;
};
template <class T>
List<T>::List(T &elem)
{
head=new Node;
head->m_elem=&elem;//修改,head->m_elem未初始化,不能用*(head->m_elem)
head->next=NULL;
}
template <class T>
List<T>::~List()//增加
{
Node* p = head, *q;
while (p)
{
q = p->next;
delete p;
p = q;
}
head=NULL;
}
int main()
{
double a=2.0;
List<double> head(a);
getchar();
return 0;
}