Reputation: 338
I'm trying to overload operator <<
in my template class and
I get error where i want to
NSizeNatural<30> a(101);
cout << a;
Without this 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
{
// this is how I 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)
{
// For now i want to have my code compile-able
return out << "gg";
}
I have no idea how to do it correctly. And where is a bug ...
Upvotes: 1
Views: 393
Reputation: 56479
You have two problems in your code, first one is, you should move the implementation of operator <<
into the header file (as the comments on your question).
But, second problem is your definition of friend
operator <<
, you must define it as a 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);
};
Upvotes: 2