有关重载友元<<的问题,麻烦各位指点一下
//LinearList.h
#include<iostream>
using namespace std;
template<class T>
class LinearList{
friend ostream& operator<<( ostream &out, const LinearList<T> &x );
public:
LinearList( int Msize = 10 );
~LinearList() { delete [] _elem; }
bool isEmpty()const { return _length == 0; }
int length()const { return _length; }
bool find( int k, T &x )const;
int search( const T &x )const;
LinearList<T>& del( int k, T &x );
LinearList<T>& insert( int k, const T &x);
private:
int _length;
int _max_size;
T *_elem;
};
template<class T>
ostream& operator<<( ostream &out, const LinearList<T> &x )
{
for( int i = 0; i < _length; ++i ){
out<< _elem[i] << ",";
}
out<< endl;
return out;
};
//test.cpp
#include<stdlib.h>
#include<iostream>
#include"LinearList.h"
using namespace std;
int main()
{
try{
LinearList<int> l(5);
cout<< " length = " << l.length() << "\n"
<< " isEmpty = " << l.isEmpty() << "\n";
l.insert( 0, 2 ).insert( 0 , 6 );
cout<< " length = " << l.length() << "\n"
<< " isEmpty = " << l.isEmpty() << "\n";
int z;
l.find( 1, z );
cout<< " first element is " << z << "\n"
<< " length = " << l.length() << "\n";
l.del(1, z );
cout<< " delete element is " << z << "\n"
<< " list is " << l << endl;
}
catch(...) {
cerr<< "an exception has occurred" << endl;
}
system( "pause" );
}
编译出错:
25 D:\cpp\LinearList.h
[Warning] friend declaration `class ostream & operator <<(ostream &, const LinearList<T> &)'
还有好多warning都是和这个友元<<有关的请各位看一下,谢谢了