gpt4 book ai didi

c++ - 为模板类重载运算符 << 时出现链接器错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:50:14 26 4
gpt4 key购买 nike

<分区>

我使用 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 : error LNK2019: unresolved external symbol "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) referenced in function "public: virtual void __thiscall log::print(class std::basic_ostream

&)const " (?print@log@@UBEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z

我该如何解决?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com