关于friend ostream

关于friend ostream - 故障解答 - 电脑教程网

关于friend ostream

日期:2006-07-21   荐:
关于friend ostream& operator<<的问题template<class T> class List{public: class Iterator;List();List(List<T>&);~List();Iterator First();Iterator Last();List<T>& operator=(List<T>&);List<T> operator (List<T>&);List<T> operator*(List<T>&);List<T> operator-(List<T>&);bool Contains(T);Iterator AddMember(T);Iterator DelMember(T);T Min();class Iterator{ public: Iterator(Node<T>* p=0); Iterator(Iterator&); Iterator& operator=(Iterator); Iterator& operator (); Iterator operator (int); Iterator& operator--(); Iterator operator--(int); T& operator*(); friend class List<T>; friend istream& operator>>(istream&, List<T>&); friend ostream& operator<<(ostream&, List<T>&); private: Node<T>* _p;};private:Node<T>* _head;Node<T>* _current;};template<class T> ostream& operator<<(ostream& ostr, List<T>& l){List<T>::Iterator it;it=l.First();while(it._p!=0) //这行出错,说不能访问it的私有成员_p,{ //但前面已经声明过友元函数了,这是为什么?ostr<<(*it);it ;}return ostr;}C:\7_7.cpp(316) : error C2248: '_p' : cannot access private member declared in class 'List<int>::Iterator' C:\7_7.cpp(45) : see declaration of '_p' C:\7_7.cpp(335) : see reference to function template instantiation '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> &)' being compiledError executing cl.exe.请问我该怎么解决?vc7.0,一切ok看错了,帮up我在6.0下没问题啊.不过执行时候好像出错了我在前面加上这些,就能通过#include <iostream>using namespace std;template<class T>class Node{};回复人: zengwujun(月之海) ( ) 信誉:90 2005-04-16 16:45:00 得分: 0 我在前面加上这些,就能通过#include <iostream>using namespace std;template<class T>class Node{}; ************呵呵,这些是理所当然要的咯.还有main函数呢楼主应该也写了,没有贴出而已.我在前面写了#include<iostream>using namespace std;template<class T> struct Node;光编译声明时没有问题,但是如果要调用ostream& operator<<(ostream&, List<T>&),比如写cout<<list;就有问题了,编译通不过Iterator& operator=(Iterator); Iterator& operator (); Iterator operator (int); Iterator& operator--(); Iterator operator--(int);这些函数都定义了吗?你把这些也贴出来咯要不别人很难给你调试的.还有,说说什么错误以下是完整程序#include<iostream>using namespace std;template<class T> struct Node;template<class T> class List{public: class Iterator;List();List(List<T>&);~List();Iterator First();Iterator Last();List<T>& operator=(List<T>&);List<T> operator (List<T>&);List<T> operator*(List<T>&);List<T> operator-(List<T>&);bool Contains(T);Iterator AddMember(T);Iterator DelMember(T);T Min();class Iterator{ public: Iterator(Node<T>* p=0); Iterator(Iterator&); Iterator& operator=(Iterator); Iterator& operator (); Iterator operator (int); Iterator& operator--(); Iterator operator--(int); T& operator*(); friend class List<T>; friend istream& operator>>(istream&, List<T>&); friend ostream& operator<<(ostream&, List<T>&); private: Node<T>* _p;};// friend istream& operator>>(istream&, List<T>&);//friend ostream& operator<<(ostream&, List<T>&);private:Node<T>* _head;Node<T>* _current;};template<class T> struct Node{Node(T data=0,Node* prev=0,Node* next=0):_data(data),_prev(prev),_next(next){}T _data;Node<T>* _prev;Node<T>* _next;};///////////////////////////////////////////////////template<class T> List<T>::Iterator::Iterator(Node<T>* p): _p(p){}template<class T> List<T>::Iterator::Iterator(Iterator& i): _p(i._p){} template<class T> List<T>::Iterator& List<T>::Iterator::operator=(Iterator i){_p=(i._p);}template<class T> List<T>::Iterator& List<T>::Iterator::operator ():{_p=_p->_next;return *this;}template<class T> List<T>::Iterator List<T>::Iterator::operator (int):{Iterator temp(this); _p=_p->_next;return temp;}template<class T> List<T>::Iterator& List<T>::Iterator::operator--():{_p=_p->_prev;return *this;}template<class T> List<T>::Iterator List<T>::Iterator::operator--(int):{Iterator temp(this); _p=_p->_prev;return temp;}template<class T> T& List<T>::Iterator::operator*(){return _p->_data;}////////////////////////////////////////////////template<class T> List<T>::List(){_head=new Node<T>();_current=0;}template<class T> List<T>::List(List<T>& l){_head=new Node<T>();_current=0;Iterator it1(_head),it2(l._head);while( it2._p){it1._p->_next=new Node<T>(*it2,it1._p,0);it1 ;}return *this;}template<class T> List<T>::~List(){while(_head->_next!=0){Node<T>* temp;temp=_head->_next;_head->_next=temp->_next;delete temp;}delete _head;}template<class T> List<T>::Iterator List<T>::First(){Iterator it(_head->_next);return it;}template<class T> List<T>::Iterator List<T>::Last(){Iterator it(_head);while(it._p->_next!=0)it ;if(it._p==_head)it._p=0;return it;}template<class T> List<T>& List<T>::operator=(List<T>& l){while(_head->_next!=0){Node<T>* temp;temp=_head->_next;_head->_next=temp->_next;delete temp;}delete _head;_head=new Node();_current=0;Iterator it1(_head),it2(l._head);while( it2._p){it1._p->_next=new Node(*it2,it1._p,0);it1 ;}return *this;}template<class T> List<T>& operator (List<T>& l){List<T> uni(*this);Iterator it1=uni.Last(),it2=l.First();if(it1._p==0)return l;else if(it2._p==0)return *this;while(it2._p!=0){it1=First();while(it1._p!=0)if((*it1)==(*it2))break;if(it1._p==0)AddMember(*it2);it2 ;}return uni}template<class T> List<T>& operator*(List<T>& l){List<T> uni;Iterator it1=Last(),it2=l.First();if(it1._p==0)return l;else if(it2._p==0)return *this;while(it2._p!=0){it1=First();while(it1._p!=0)if((*it1)==(*it2))break;if(it1._p!=0)uni.AddMember(*it2);it2 ;}return uni}template<class T> List<T>& operator-(List<T>& l){List<T> uni(*this);Iterator it1=uni.Last(),it2=l.First();if(it1._p==0)return l;else if(it2._p==0)return *this;while(it2._p!=0){it1=First();while(it1._p!=0)if((*it1)==(*it2))break;if(it1._p!=0)uni.DelMember(*it2);it2 ;}return uni}template<class T> bool List<T>::Contains(T x){Iterator it=First();while(it._p!=0)if((*it)==x)break;if(it!=0)return 1;elsereturn 0;}template<class T> List<T>::Iterator List<T>::AddMember(T x){Iterator it(_head);while(it._p->_next!=0)it ;it._p->_next=new Node(x,it._p,0);it ;return it;}template<class T> List<T>::Iterator List<T>::DelMember(T x){Iterator it(_head);while(it._p->_next!=x && it._p!=0)it ;if(*it==x){Node* temp=it._p->_next; it._p->_next=temp->_next;delete temp;}return it;}template<class T> T Min(){T min=0;Iterator it;it=First();while(it._p!=0){if(*it<min)min=*it;it ;}return min;}/////////////////////////////////////////template<class T> istream& operator>>(istream& istr, List<T>& l){T temp;char flag;cout<<"please input the data that you want to be added to the list"<<endl;cout<<"do you want to go on?(Y/N) : ";cin>>flag;while(flag=='Y' || flag=='y'){cout<<endl<<"data : ";cin>>temp; l.AddMember(temp);cout<<"do you want to go on?(Y/N) : ";cin>>flag;}}template<class T> ostream& operator<<(ostream& ostr, List<T>& l){List<T>::Iterator it;it=l.First();while(it._p!=0){ostr<<(*it);it ;}return ostr;}void main(){List<int> list1,list2;cout<<"please input the contents of list 1"<<endl;cin>>list1;cout<<"please input the contents of list 2"<<endl;cin>>list2;cout<<"now the contents of the 2 lists are : "<<endl;cout<<"list1 : ";cout<<list1;cout<<"list2 : ";cout<<list2;}错误就是最前面说的,只有这一个错误我也不明白了,照常规应该可以的各位大侠帮忙看一下我再顶一下,请各位帮忙看看。多谢啦能力有限爱莫能助~~我在Iterator类里面添加多了一个友元函数,却不会有问题.为什么operator<<会有问题呢?还得想想.**friend void Friend(List<T>& l);--------template<class T>void Friend(List<T>& l){List<T>::Iterator it = l.First();cout<<it._p<<endl;} sp5?sp6这是VC6的问题,在友元模板实例化的时候经常会出问题建议用别的编译工具试一下.参考stl原码nod, 似乎 vc6 对新标准的支持有不少不兼容性。。可以考虑换个编译器,比如 gcc 啥 的。
标签: