c++ - friend function in class templates and error LNK2019 -


i'm trying overload operator << in template class , error want

nsizenatural<30> a(101); cout << a; 

without whole program compiles

error:

error lnk2019: unresolved external symbol "class   std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<<30,unsigned int,unsigned __int64,10>(class std::basic_ostream<char,struct std::char_traits<char> > &,class nsizenatural<30,unsigned int,unsigned __int64,10> const &)" (??$?6$0bo@i_k$09@@yaaav?$basic_ostream@du?$char_traits@d@std@@@std@@aav01@abv?    $nsizenatural@$0bo@i_k$09@@@z) referenced in function _main 

my numbers.h file :

template<int size, typename basic_type = unsigned int, typename long_type = unsigned long long, long_type base = bases(dec)>  class nsizenatural {        // how decelerate friend function        friend ostream& operator<<(ostream& str, const nsizenatural<size, basic_type, long_type, base> & n); } 

in numbers.cpp file :

template<int size, typename basic_type, typename long_type, long_type base> std::ostream& operator<<(std::ostream& out, const nsizenatural<size, basic_type,   long_type, base> &y) {    // want have code compile-able     return out << "gg";  } 


have no idea how correctly. , bug ...

you have 2 problems in code, first 1 is, should move implementation of operator << header file (as comments on question).

but, second problem definition of friend operator <<, must define template function without default parameter:

template<int size, typename basic_type = unsigned int, typename long_type = unsigned long long, long_type base = bases(dec)> class nsizenatural {     template<int size1, typename basic_type1, typename long_type1, long_type base1>     friend ostream& operator<< (ostream& str, const nsizenatural<size, basic_type, long_type, base> & n); }; 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -