2013-08-24 69 views
0

我試圖超載<<爲包含對象的數組MemSet模板...但如果我試圖做的MemSetMemSet則超載不起作用超載<<在C基於模板對象++

///// 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類

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

太多的代碼 - 你爲什麼不縮小顯示的問題,這個問題和後最小碼? – user93353

+0

好的,我不是很確定爲什麼錯誤在那裏,所以我張貼了大部分 –

+0

@ParagJain - 學習調試 - 使人們縮小一點 –

回答

1

錯誤添加 'MSET' 到 'MM1' mm1 << mset代替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; 
    } 
}; 

但是,在添加「句法su」時,我會小心CuteProgramming噶「這樣的代碼。

+0

謝謝你的工作,你能建議一些很好的參考或模板上的教程我需要添加一些更多的功能,如重載新和[] == MemSet ..... –

+0

這裏有一個很好的主題走在這裏的主題SO:[運算符重載](http://stackoverflow.com/questions/4421706/operator-overloading) – such

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; 
} 
+0

我不能使用載體,但感謝這一點,它會幫助我理解.... –