【问题标题】:error C2955: 'ListRemake' : use of class template requires template argument list错误 C2955:“ListRemake”:使用类模板需要模板参数列表
【发布时间】:2010-11-21 21:42:59
【问题描述】:
template <class T>
class ListRemake
{
    ...
    friend ostream& operator << (ostream& out, const ListRemake& obj);
};

template <class T>
ostream& operator << (ostream& out, const ListRemake& obj)
{
    for (int i = 0; i < obj.size; i++)
        out << obj[i] << '\n';
    return out;
}

给出错误 C2955: 'ListRemake' : 使用类模板需要模板参数列表。

【问题讨论】:

    标签: c++ templates function-templates


    【解决方案1】:

    替换

    ostream& operator << (ostream& out, const ListRemake& obj)
    

    ostream& operator << (ostream& out, const ListRemake<T>& obj)
    

    【讨论】:

    • 我得到一个链接器错误:未解决的错误:错误 1 ​​错误 LNK2019:未解析的外部符号“class std::basic_ostream> & __cdecl operator > &,class ListRemake const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@ @AAV01@ABV?$ListRemake@N@@@Z) 在函数_main中引用
    • @cable:您是否试图将不同文件中的声明和定义分开?这不适用于模板。
    • 不,它和 main() 都在同一个文件中,如果你想看看 pastebin.com/E9VkcDNH,我将整个代码粘贴到 pastebin 上
    • @cable 你错过了你的朋友声明的“模板”......否则它声明了一个非模板函数......
    【解决方案2】:

    错误告诉您ListRemake 是一个模板,因此您需要实例化它以将其用作类型(您在&lt;&lt; 运算符中执行的操作)。

    【讨论】:

    • 我不确定我是否理解。我做错了什么,我应该改变什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多