C++ 超载<&书信电报;对于c++;班
我试图为包含对象数组的codeMemSet/code模板重载C++ 超载<&书信电报;对于c++;班,c++,templates,operator-overloading,C++,Templates,Operator Overloading,我试图为包含对象数组的codeMemSet/code模板重载r; cin>>r2; cin>>r3; mset.add(r); mset.add(r2); mset.add(r3); coutr5; cin>>r6; mset2.添加(r); mset2.add(r2); mset2.添加(r3); 如果(mset==mset2) cout使用语法mm1将'mset'添加到'mm1'这里有一个示例,它提供了类似于我假设您想要的行为。您需要扩展类以提供比较 #include <iostrea
>r; cin>>r2; cin>>r3; mset.add(r); mset.add(r2); mset.add(r3); coutr5; cin>>r6; mset2.添加(r); mset2.add(r2); mset2.添加(r3); 如果(mset==mset2)
cout使用语法mm1将'mset'添加到'mm1'这里有一个示例,它提供了类似于我假设您想要的行为。您需要扩展类以提供比较
#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; }
#包括 #包括 #包括 使用名称空间std; 课堂记录 { friend istream&operator>>(istream&s、Record&r); friend ostream&operator>(istream&s、Record&r) { s>>r.m_ènAge; s>>r.m_strFirstName; s>>r.m_strLastName; 返回s; }
ostream&operatorWay太多的代码-为什么不缩小问题的范围并发布显示问题的最小代码?好的,我不太确定为什么会出现错误,所以我发布了大部分错误part@ParagJain-学会调试-使人能够缩小调试范围little@EdHeal你如何调试一个没有编译的程序?当你编写mm1时好了,你能推荐一些关于模板的好参考或教程吗?我需要在这里添加更多的函数,比如重载new和[]==for 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>&}’
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; } };
#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; }