首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

在模版中重载<< error LNK2019解决思路

2012-02-28 
在模版中重载 error LNK2019C/C++ code//main.cpp#includelist.hvoid main(){Listint aint i1a.I

在模版中重载<< error LNK2019

C/C++ code
//main.cpp#include"list.h"void main(){    List<int> a;    int i=1;    a.Insert(i);    a.Insert(2);    a.Insert(3);    a.Del(3);    a.Find(2);    cout<<a;    cin.get();}

C/C++ code
#include<iostream>#include"node.h"using namespace std;template <class Type> class List{public:    friend ostream & operator <<(ostream &,List<Type> &);    List() {first=new Node<Type>;last=first;num=0;}    List(const List<Type> &);    ~List();    void Insert(Type);    void Insert(Node<Type> &);    void Insert(Node<Type> *);    Node<Type> *Find(Type);    void Del(Type);    void Del(Node<Type> *);    void MakeEmpty();    Node<Type> *GetFirst() {return first;}    Node<Type> *GetLast() {return last;}    int &GetNum() {return num;}private:    Node<Type> *first,*last;    int num;};template <class Type> List<Type>::List(const List<Type> &rhs){    num=rhs.GetNum();    if(num==0)    {        first=new Node<Type>;        last=first;    }    else    {        first=new Node<Type>;        last=first;        Node<Type> *NewNode=0;        Node<Type> *front=first;        for(int i=1;i<=num;i++)        {            NewNode=rhs.GetFirst()->link;            front->link=NewNode;            front=front->link;        }        last=front;    }}template <class Type> List<Type>::~List(){    MakeEmpty();    delete first,last;}template <class Type> void List<Type>::Insert(Type rhs){    Node<Type> *NewNode=new Node<Type>(rhs);     if(num==0)        first->link=NewNode;    else        last->link=NewNode;    last=NewNode;    num++;}template <class Type> void List<Type>::Insert(Node<Type> *rhs){    if(num==0)        first->link=rhs;    else        last->link=rhs;    rhs->link=0;    last=rhs;    num++;}template <class Type> Node<Type> *List<Type>::Find(Type rhs){    Node<Type> *p=first->link;    while(p!=last && p->data!=rhs)        p=p->link;    if(p->data==rhs)        return p;    else        return first;}template <class Type> void List<Type>::Del(Type rhs){    Node<Type> *p=Find(rhs);    if(p!=first)    {        Node<Type> *front=first->link;        while(front->link!=p)            front=front->link;        front->link=p->link;        if(p==last)            last=front;        delete p;        num--;    }}template <class Type> void List<Type>::Del(Node<Type> *rhs){    if(rhs!=first)    {        Node<Type> *front=first;        while(front->link!=rhs)            front=front->link;        front->link=rhs->link;        if(rhs==last)            last=front;        delete rhs;        num--;    }}template <class Type> void List<Type>::MakeEmpty(){    last=first->link;    while(first!=0)    {        first=last->link;        delete last;        last=first;    }    first=new Node<Type>;    num=0;}template <class Type> ostream & operator <<(ostream &os,List<Type> &rhs){    if(rhs.GetNum()==0)        os<<"The List is Empty"<<endl;    else    {        int i=1;        Node<Type> *p=rhs.GetFirst()->link;        do        {            os<<"第"i<<"个元素:"<<p->data;            i++;            p=p->link;            if(i%50==0)                cout<<endl;        }while(p!=last)    }    return os;}


1>main.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class List<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$List@H@@@Z),该符号在函数 _main 中被引用
1>d:\Users\Administrator\Documents\Visual Studio 2008\Projects\单链表\Debug\单链表.exe : fatal error LNK1120: 1 个无法解析的外部命令

[解决办法]
无 "Node<>" 贴出来,我也不知道怎么回事??
[解决办法]
friend template<class Type>ostream & operator <<(ostream &,List<Type> &);//加上试试

[解决办法]
珍惜生命,远离VC6

热点排行