0

我正在尝试为包含对象数组的模板重载<<...MemSet但是如果我尝试这样做MemSetMemSet则重载不起作用

///// 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>&}’
4

2 回答 2

1

这是一个示例,它提供了类似于我认为您想要的行为。您将需要扩展类以提供比较。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Record
{
    friend istream& operator>> (istream& s, Record& r);
    friend ostream& operator<< (ostream& s, Record& r);

public:
    int GetAge(void) const { return m_nAge; }
    const std::string& GetFirstName(void) const { return m_strFirstName; }
    const std::string& GetLastName(void) const { return m_strLastName; }

private:
    int m_nAge;
    string m_strFirstName;
    string m_strLastName;
};

istream& operator>> (istream& s, Record& r)
{
    s >> r.m_nAge;
    s >> r.m_strFirstName;
    s >> r.m_strLastName;
    return s;
}

ostream& operator<< (ostream& s, Record& r)
{
    s << "Individual: " << r.m_strLastName << ", "
        << r.m_strFirstName << " (" << r.m_nAge << " years)" << endl;
    return s;
}

template<class T>
class MemSet : public vector<T>
{
    template<class T1>
    friend ostream& operator<< (ostream& s, MemSet<T1>& m);

    template<class T1>
    friend MemSet<T1>& operator<< (MemSet<T1>& m, T1& t);
};

template<class T>
ostream& operator<< (ostream& s, MemSet<T>& m)
{
    for (typename vector<T>::iterator it = m.begin(); it != m.end(); ++it)
        s << *it;
    return s;
}

template<class T>
MemSet<T>& operator<< (MemSet<T>& m, T& t)
{
    m.push_back(t);
    return m;
}

int main(int argc, char** argv)
{
    MemSet<Record> mset1, mset2;
    MemSet< MemSet<Record> > mm;
    Record temp;

    cin >> temp; mset1.push_back(temp);
    cin >> temp; mset1.push_back(temp);
    cin >> temp; mset1.push_back(temp);

    cin >> temp; mset2.push_back(temp);
    cin >> temp; mset2.push_back(temp);
    cin >> temp; mset2.push_back(temp);

    cout << mset1 << endl;

    mm << mset1;
    mm << mset2;

    cout << mm << endl;

    return 0;
}
于 2013-08-24T11:16:09.390 回答
1

mm1 << mset使用语法而不是添加 'mset' 到 'mm1'mm1.add(mset)可以通过以下方式完成:

template<typename T>
MemSet<T>& operator << (MemSet<T>& m, const T& v ) {
    m.add (v);
    return m;
}

或同等学历

template<typename T>
class MemSet: public Set {
public:
    MemSet& operator << (const T& v ) {
        add (v);
        return *this;
    }
};

但是,在像这样向您的代码中添加“语法糖”时,我会小心可爱编程。

于 2013-08-24T11:05:51.777 回答