本文介绍了C ++模板和朋友,链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下代码无法编译(链接器错误,未解析的外部符号, class std :: basic_ostream< char,struct std :: char_traits< char>& __cdecl运算符<<( class std :: basic_ostream< char,struct std :: char_traits< char>& ;,类cClass< int> const&)(?? 6 @ YAAEAV?$ basic_ostream @ DU?$ char_traits @ D @ std @ @@ std @@ AEAV01 @ AEBV?$ cClass @ H @@@ Z)
)
Why does the following code not compile (linker error, unresolved external symbol, class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class cClass<int> const &)" (??6@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV01@AEBV?$cClass@H@@@Z)
)
#include <iostream>
template <class Type> class Class{
public:
friend std::ostream& operator<<(std::ostream& Stream, const Class& Op);
};
template <class Type> std::ostream& operator<<(std::ostream& Stream, const Class<Type>& Op){
return(Stream);
}
int main(){
Class<int> A;
std::cout << A;
return(0);
}
推荐答案
此行:
friend std::ostream& operator<<(std::ostream& Stream, const Class& Op);
应该是:
template <class T> friend std::ostream& operator<<(std::ostream& Stream, const Class<T>& Op);
这篇关于C ++模板和朋友,链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!