这个问题在这里已有答案:
我使用visual c . 我有一个模板类,我想为它添加重叠操作我像下面的头文件一样阻止它
template <class T> class QuantityT;
template <class T>
inline std::ostream & operator<< (std::ostream & os,const QuantityT<T> &quantity);
template <class T>
class QuantityT{
T quantity_;
template<class T> friend inline std::ostream & operator<< <T>(std::ostream & os,const QuantityT<T> &quantity);
};
在源文件中
template <class T>
inline std::ostream & operator<< (std::ostream & os,const QuantityT<T> &quantity){
}
但我收到链接错误:
main.obj:错误LNK2019:未解析的外部符号“class std :: basic_ostream>&__ cdecl operator <<(class std :: basic_ostream>&,class QuantityT const&)”(?? $?6K @@ YAAAV?$ basic_ostream @DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABV?$ QuantityT @ K @@@ Z)在函数“public:virtual void __thiscall log :: print(class std :: basic_ostream&)const”中引用“(?print @ log @@ UBEXAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@@ Z
我怎么解决它?
1 回答
尝试将源文件的内容放入头文件中:
有关更多信息,请查看this question .