【问题标题】:how to remove error of output opertor overloading of a template class如何消除模板类的输出运算符重载错误
【发布时间】:2026-01-19 14:35:01
【问题描述】:
friend ostream& operator<< (ostream& os,const aList<T>&);

template <class T>
ostream & operator << (ostream &output,const aList<T>& a){
    output<<a.length;
    return output; 
}

.....................................

error: g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/aList.o.d" -o build/Debug/Cygwin-Windows/aList.o aList.cpp
In file included from aList.cpp:2:0:
aList.h:22:68: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const aList<T>&)’ declares a non-template function [-Wnon-template-friend]
             friend ostream& operator<< (ostream& os,const aList<T>&);
                                                                    ^
aList.h:22:68: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/alist build/Debug/Cygwin-Windows/_ext/2ebfcef6/newstring.o build/Debug/Cygwin-Windows/aList.o build/Debug/Cygwin-Windows/alist1.o 
build/Debug/Cygwin-Windows/alist1.o: In function `main':
/cygdrive/c/Users/Zunera Jamal/Documents/NetBeansProjects/aList/alist1.cpp:50: undefined reference to `operator<<(std::ostream&, aList<newstring> const&)'
/cygdrive/c/Users/Zunera Jamal/Documents/NetBeansProjects/aList/alist1.cpp:50:(.text+0x2ca): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `operator<<(std::ostream&, aList<newstring> const&)'

【问题讨论】:

标签: c++


【解决方案1】:

乍一看,您的朋友声明所指但在模板声明之外这一事实似乎是您的第一个问题。

看看这个问题。这似乎非常相关: friend declaration declares a non-template function

【讨论】:

    最近更新 更多