【问题标题】:How do I serialise a template class如何序列化模板类
【发布时间】:2014-02-27 16:48:46
【问题描述】:

我正在对遗留代码执行一些后台维护。

它涉及用 STL 替换已弃用的库,并在保持界面尽可能相似的同时进行提升。

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

binary '

我猜编译器之所以抱怨是因为它事先不知道 T 是否可序列化?但是,这在当前库中有效 - 我错过了什么吗?

【问题讨论】:

  • 尝试删除class

标签: c++ templates serialization


【解决方案1】:

您可以尝试:(添加template &lt;typename T&gt;,删除class)。

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();
    }
}

【讨论】:

  • 还取决于T的类型。 T 可能在不为它重载运算符的情况下无法插入。
猜你喜欢
  • 2010-11-22
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 2011-07-26
相关资源
最近更新 更多