c++求解释,怎么改,老是提示undefined reference to operator<<(std::ostream,const)
#include<iostream>usingnamespacestd;template<classelemType>classlinkList;template<cla...
#include<iostream>
using namespace std;
template<class elemType> class linkList;
template<class T> ostream&operator<<(ostream&,const linkList<T>&);
template<class elemType>class Node;
template<class elemType>
class Node
{
friend class linkList<elemType>;
friend ostream &operator<<(ostream&,const linkList<elemType>&);
private:
elemType data;
Node<elemType>*next;
public:
Node(const elemType &x,Node<elemType>*N=NULL){data=x;next=N;}
Node():next(NULL){}
~Node(){}
};
template<class elemType>
class linkList
{
friend ostream&operator<<(ostream&,const linkList<elemType>&);
protected:
Node<elemType>*head;
int makeEmpty();
public:
linkList(){head=new Node<elemType>;}
~linkList(){makeEmpty();delete head;}
int create(const elemType &flag);
};
template<class elemType>
int linkList<elemType>::makeEmpty()
{
Node<elemType>*p=head->next,*q;
head->next=NULL;
while(p!=NULL){q=p->next;delete p;p=q;}
return 0;
}
template <class elemType>
int linkList<elemType>::create(const elemType &flag)
{
elemType tmp;
Node<elemType>*p,*q=head;
cout<<"input the data of the link list "<<flag<<"to represent the end "<<endl;
while(true)
{
cin>>tmp;
if(tmp==flag)break;
p=new Node<elemType>(tmp);
q->next=p;
q=p;
}
return 0;
}
template <class T>
ostream &operator<<(ostream &os,const linkList<T>&obj)
{
Node<T>*q=obj.head->next;
os<<endl;
while(q!=NULL){os<<q->data;q=q->next;}
return os;
}
int main()
{
linkList<int> a;
a.create(1);
a.create(2);
a.create(3);
a.create(5);
a.create(8);
a.create(13);
a.create(0);
cout<<a;
return 0;
} 展开
using namespace std;
template<class elemType> class linkList;
template<class T> ostream&operator<<(ostream&,const linkList<T>&);
template<class elemType>class Node;
template<class elemType>
class Node
{
friend class linkList<elemType>;
friend ostream &operator<<(ostream&,const linkList<elemType>&);
private:
elemType data;
Node<elemType>*next;
public:
Node(const elemType &x,Node<elemType>*N=NULL){data=x;next=N;}
Node():next(NULL){}
~Node(){}
};
template<class elemType>
class linkList
{
friend ostream&operator<<(ostream&,const linkList<elemType>&);
protected:
Node<elemType>*head;
int makeEmpty();
public:
linkList(){head=new Node<elemType>;}
~linkList(){makeEmpty();delete head;}
int create(const elemType &flag);
};
template<class elemType>
int linkList<elemType>::makeEmpty()
{
Node<elemType>*p=head->next,*q;
head->next=NULL;
while(p!=NULL){q=p->next;delete p;p=q;}
return 0;
}
template <class elemType>
int linkList<elemType>::create(const elemType &flag)
{
elemType tmp;
Node<elemType>*p,*q=head;
cout<<"input the data of the link list "<<flag<<"to represent the end "<<endl;
while(true)
{
cin>>tmp;
if(tmp==flag)break;
p=new Node<elemType>(tmp);
q->next=p;
q=p;
}
return 0;
}
template <class T>
ostream &operator<<(ostream &os,const linkList<T>&obj)
{
Node<T>*q=obj.head->next;
os<<endl;
while(q!=NULL){os<<q->data;q=q->next;}
return os;
}
int main()
{
linkList<int> a;
a.create(1);
a.create(2);
a.create(3);
a.create(5);
a.create(8);
a.create(13);
a.create(0);
cout<<a;
return 0;
} 展开
展开全部
标明一下哪一句报错。
追问
linklist类里的friend ostream&operator&);
追答
template
ostream &operator&obj)
{
Node*q=obj.head->next;
osdata;q=q->next;}
return os;
}
注意友元函数本身不是模板函数,必须显示定义其具体化
//template
ostream &operator&obj)
{
Node*q=obj.head->next;
osdata;q=q->next;}
return os;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询