C++ 如何序列化模板类
我正在对遗留代码执行一些后台维护 它包括用STL和boost替换不推荐使用的库,同时尽可能保持界面的相似性C++ 如何序列化模板类,c++,templates,serialization,C++,Templates,Serialization,我正在对遗留代码执行一些后台维护 它包括用STL和boost替换不推荐使用的库,同时尽可能保持界面的相似性 ostream& operator<<(ostream& vos, const OurList<class T>& coll) { // OurList wraps an stl list with the same interface as the current library // OurListIterator wra
ostream& operator<<(ostream& vos, const OurList<class T>& coll)
{
// OurList wraps an stl list with the same interface as the current library
// OurListIterator wraps an iterator class used to access OurList
OurListIterator<T, OurList<class T> > iter((OurList<class T>&)coll);
// this function gets the first item in the list and then each next one
while (iter())
{
// key returns the value pointed to by the iterator
vos << iter.key();
}
};
然而,当我编译它时,我在vos上得到一个错误您可以尝试:添加模板,删除类
尝试删除类。这还取决于T的类型。如果不为其重载运算符,T可能无法插入。
template <typename T>
ostream& operator<<(ostream& vos, const OurList<T>& coll)
{
// OurList wraps an stl list with the same interface as the current library
// OurListIterator wraps an iterator class used to access OurList
OurListIterator<T, OurList<T> > iter((OurList<T>&)coll);
// this function gets the first item in the list and then each next one
while (iter())
{
// key returns the value pointed to by the iterator
vos << iter.key();
}
}