我正在尝试为包含对象数组的模板重载<<
...MemSet
但是如果我尝试这样做MemSet
,MemSet
则重载不起作用
///// SetMain.cpp
int main()
{
MemSet<Record> mset(10),mset2(10);
MemSet< MemSet<Record> >mm1(10);
Record r,r2,r3,r4,r5,r6;
cin>>r;
cin>>r2;
cin>>r3;
mset.add(r);
mset.add(r2);
mset.add(r3);
cout<<" Memset 2:"<<endl;
cin>>r4;
cin>>r5;
cin>>r6;
mset2.add(r);
mset2.add(r2);
mset2.add(r3);
if(mset == mset2)
cout<<"Both memset are equal"<<endl;
else
cout<<"Memsets are not equal"<<endl;
mm1<<mset;
//mset2>>mm2;
if(mm1 == mm2)
cout<<"Both memset of memset are equal"<<endl;
else
cout<<"Memset of memset are not equal"<<endl;
return 0;
}
这是 Memset 类....
////Set and MemSet class
template <class T>
class Set
{
public :
virtual bool find(T x) =0;
virtual bool add(T x) =0;
};
template<class T>
class MemSet : public Set<T>
{
public:
T *array;
int arraysize,currentsize;
template <class T1>
friend istream& operator >> (istream& s,MemSet<T1>& m );
template <class T1>
friend ostream& operator << (ostream& s,MemSet<T1>& m );
};
template <class T>
ostream& operator << (ostream& s,MemSet<T>& m )
{
for(int i=0;i<m.getCurrentsize();i++)
s << m.array[i];
return s;
}
记录类
////Record class
class Record
{
public:
int age;
std::string first_name,last_name;
friend istream& operator >> (istream& s,Record& r );
friend ostream& operator << (ostream& s,Record& r );
};
istream& operator >> (istream& s,Record& r )
{
s>>r.age;
s>>r.first_name;
s>>r.last_name;
return s;
}
ostream& operator << (ostream& out,Record& r )
{
out << r.age ;
out << r.first_name ;
out << r.last_name ;
return out ;
}
这些是我得到的错误
SetMain.cpp: In function ‘int main()’:
SetMain.cpp:36:6: error: no match for ‘operator<<’ in ‘mm1 << mset’
SetMain.cpp:36:6: note: candidates are:
Set.cpp:111:10: note: template<class T> std::ostream& operator<<(std::istream&, MemSet<T>&)
Record.cpp:46:10: note: std::ostream& operator<<(std::ostream&, Record&)
Record.cpp:46:10: note: no known conversion for argument 1 from ‘MemSet<MemSet<Record> >’ to ‘std::ostream& {aka std::basic_ostream<char>&}’