c++ - Explicit instantiation of operator>> overload -
i have class vector<t>
, , using library provides class yaml::node
. overload operator>>
these 2 types.
i have added following declaration vector
's declaration:
friend void operator>>(yaml::node const & node, vector<t> & v);
i have added following implementation of function:
template<typename t> void operator>>(yaml::node const & node, vector<t> & v) { node[0] >> v.x; node[1] >> v.y; node[2] >> v.z; }
finally, have added following (attempt at) explicitly instantiating template t = num_t
:
template void operator>>(yaml::node const & node, vector<num_t> & v);
however, results in following linker error:
error 9 error lnk2019: unresolved external symbol "void __cdecl operator>>(class yaml::node const &,class vector<double> &)" (??5@yaxaebvnode@yaml@@aeav?$vector@n@@@z) referenced in function "public: static class scene __cdecl scene::fromfile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?fromfile@scene@@sa?av1@v?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@@z)
(num_t
typedef double
)
however, if add following (non-template) implementation of function, compiles fine:
void operator>>(yaml::node const & node, vector<num_t> & v) { node[0] >> v.x; node[1] >> v.y; node[2] >> v.z; }
why template version of function not working?
edit: forgot mention; compiler visual studio 11 beta
declaring function friend
not declare function template; instead, each specialisation of class template declares non-template function, parameter types overloaded according template arguments. these chosen instead of template define; not defined, hence error.
to fix it, can either declare function template before class template (in case friend declaration make friend, rather declare new function), or define friend function inline, inside class template, each specialisation of class template defines function declaring it.
Comments
Post a Comment