c++ - error: no matching function for call to second::second, candidates are: second::second( -
c++ - error: no matching function for call to second::second, candidates are: second::second( -
i need homecoming proper type per templatized argument. getting error below: can please suggest whats solution this? in advance.
error: no matching function phone call âsecond::second(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)â note: candidates are: second::second(const std::string&, const std::string&) note: second::second(const second&)
code below:
struct first { public: const string &str; first(const string & str) : str(str) { } }; struct sec : public first { public: const string &str2; second(const string &str1, const string &str2) : first(str1), str2(str2) { } }; class base of operations { public: template<class t> inline t fun(const string &s1, const string &s2);// { cout<<" t = "<<a; } }; template<class t> inline t base::fun(const string &s1, const string &s2) { if(1) homecoming t(s1); else homecoming t(s1, s2); } int main() { string = "a"; string bb = "b"; base of operations b; b.fun<first>(a, bb); b.fun<second>(a, bb); homecoming 0; }
the problem can't create function template accepts 2 arguments of fixed types, , returns objects of different types depending on template parameter. reason can't specialize template functions, can overload them, , can't create overloaded functions differ homecoming type.
what can utilize sfinae. way @ 1 function nowadays given template parameter:
class base of operations { public: template<typename t, typename = typename std::enable_if<std::is_same<t, first>::value>::type> first fun(const string &s1, const string &s2) { homecoming first(s1); } template<typename t, typename = typename std::enable_if<std::is_same<t, second>::value>::type> sec fun(const string &s1, const string &s2) { homecoming second(s1, s2); } };
alternatively can create base
templated , specialize it:
template<typename t> class base; template<> class base<first> { public: static first fun(const string &s1, const string &s2) { homecoming first(s1); } }; template<> class base<second> { public: static sec fun(const string &s1, const string &s2) { homecoming second(s1, s2); } }; base<first>::fun(a, bb); base<second>::fun(a, bb);
demo
c++ inheritance
Comments
Post a Comment