C++的模板链表类问题,只有20分全给上,在线等答案,求高手破解,忽悠的闪

//Lnode.h文件

#pragma once
template<class T>
class Lnode
{
public:
T data;
Lnode<T>* next;
Lnode(int,Lnode<T>*);
~Lnode(void);
};

//Lnode.cpp文件

#include "Lnode.h"

template<class T>
Lnode<T>::Lnode(int el,Lnode<T>* ne):data(el),next(ne)
{
}

template<class T>
Lnode<T>::~Lnode(void)
{
}

//linkedlist.h文件

#pragma once
#include"Lnode.h"
template<class T>
class linkedlist
{
public:
linkedlist(void);
~linkedlist(void);
void pushfront(int);
void print();
private:
Lnode<T> *head;
unsigned int t;
};

//linkedlist.cpp文件

#include "linkedlist.h"
#include<iostream>
using namespace std;
template<class T>
linkedlist<T>::linkedlist(void)
{
head=0;
t=0;
}

template<class T>
linkedlist<T>::~linkedlist(void)
{
Lnode<T> * buf;
while(t!=0)
{
buf=head;
head=head->next;
delete buf;
t--;
}

}

template<class T>
void linkedlist<T>::pushfront(int el)
{
head=new Lnode<T>(a,head);
t++;
}

template<class T>
void linkedlist<T>::print()
{
Lnode<T>* buf_head=head;
while(buf_head)
{
cout<<buf_head->data;
buf_head=buf_head->next;
}
}

//主函数文件

#include"linkedlist.h"
void main()
{
linkedlist<int> list;
list.pushfront(9);
list.pushfront(6);
list.pushfront(5);
list.print();
}

//错误信息:

1>template.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall linkedlist<int>::~linkedlist<int>(void)" (??1?$linkedlist@H@@QAE@XZ),该符号在函数 _main 中被引用
1>template.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall linkedlist<int>::print(void)" (?print@?$linkedlist@H@@QAEXXZ),该符号在函数 _main 中被引用
1>template.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall linkedlist<int>::pushfront(int)" (?pushfront@?$linkedlist@H@@QAEXH@Z),该符号在函数 _main 中被引用
1>template.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall linkedlist<int>::linkedlist<int>(void)" (??0?$linkedlist@H@@QAE@XZ),该符号在函数 _main 中被引用
1>c:\users\sunny\documents\visual studio 2010\Projects\template\Debug\template.exe : fatal error LNK1120: 4 个无法解析的外部命令

目前c++不支持模板的定义和实现分离。也就是模板类或模板函数的声明和实现必须放在同一个文件当中。不能声明在.h中,实现在cpp中。

具体看看
http://blog.csdn.net/thinkscape/archive/2008/12/20/3567420.aspx

所以你只要把声明和实现都放到一个文件中就行了。
温馨提示:答案为网友推荐,仅供参考
相似回答