我正在尝试<<
在模板类中重载运算符,但我想在哪里出错
NSizeNatural<30> a(101);
cout << a;
没有整个程序就编译
错误:
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
我的Numbers.h文件:
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);
}
在Numbers.cpp文件中:
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";
}
我不知道如何正确地做。还有一个错误...
您的代码中有两个问题,第一个是,您应该将实现operator <<
移到头文件中(作为对问题的注释)。
但是,第二个问题是您对的定义friend
operator <<
,您必须将其定义为没有默认参数的模板函数:
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);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句