C++ 泛型编程常见问题类模板实现友元函数报错

泛型编程友元函数编译器报错

在实现模板类的时候, 在类的内部声明一个友元friend 函数时,请一定加上类模板声明template

template <typename T>
	friend ostream& operator<<(ostream& os, const Vector<T> object);

这样可以避免C++编译器报错,同样在友元函数的实现时也要加上此声明.

可以避免如下报错 :
error LNK2019: 无法解析的外部符号 “class std::basic_ostream<char,struct std::char_traits > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits > &,class Vector)” (??6@YAAAV? b a s i c o s t r e a m @ D U ? basic_ostream@DU? basicostream@DU?char_traits@D@std@@@std@@AAV01@V?$Vector@H@@@Z),该符号在函数 _main 中被引用

fatal error LNK1120: 1 个无法解析的外部命令

猜你喜欢

转载自blog.csdn.net/u011213040/article/details/104085938