簡體   English   中英

我如何序列化模板類

[英]How do I serialise a template class

我正在對遺留代碼進行一些后台維護。

它涉及到用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 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 << iter.key()行出現錯誤:

二進制'<<':未找到采用類型為'T'的右側操作數的運算符(或沒有可接受的轉換)

我猜編譯器在抱怨,因為它不預先知道T是否可序列化? 但是,這在當前庫中有效-我缺少什么嗎?

您可以嘗試:(添加template <typename T> ,刪除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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM