3

I use visual c++. I have a template class and I want add overlapped operation for it I impelement it like below in header file

    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);
    };

in souce file

    template <class T>
    inline std::ostream & operator<< (std::ostream & os,const QuantityT<T> &quantity){
    }

but I get link error:

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

How I can fix it?

masoud
  • 55,379
  • 16
  • 141
  • 208
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49

1 Answers1

3

Try putting the source file's contents into the header file:

template <class T>
inline std::ostream & operator<< (std::ostream & os,const QuantityT<T> &quantity){
}

Check this question out for more information.

Community
  • 1
  • 1
Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123