templates - How to implement parent class with templated function in c++? -
i have folllowing problem:
i want implement next construction of classes:
parent in iparser.h
#ifndef iparser_h #define iparser_h #include "json.h" class iparser { public: template <typename t> json::object parse(const t&, json::object); }; #endif // iparser_h
child in htmlparser.h
#ifndef htmlparser_h #define htmlparser_h #include <iostream> #include "iparser.h" class htmlparser : public iparser { public: htmlparser(); ~htmlparser(); json::object parse(std::string const&, json::object&); }; #endif
child in htmlparser.cpp
#include "htmlparser.h" htmlparser::htmlparser() { std::cout << "constructed" << std::endl; } htmlparser::~htmlparser() { std::cout << "destructed" << std::endl; } json::object htmlparser::parse(std::string const& data, json::object& object) { // homecoming json::object(); }
but when want build it, throws me error:
error lnk2019: unresolved external symbol "public: class json::object __thiscall iparser::parse<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class json::object)" (??$parse@v?$basic_string@du? $char_traits@d@std@@v?$allocator@d@2@@std@@@iparser@@qae?avobject@json@@abv?$basic_string@du? $char_traits@d@std@@v?$allocator@d@2@@std@@v12@@z) referenced in function _main
any thought wrong? want create interface class templated function kid classes specify , implement.
any help appreciated. thanks.
first let's see error trying tell you, not eloquently. error coming linker
error lnk2019: unresolved external symbol
so compiler ok code, created dependency symbol linker did not find. symbol
"public: class json::object __thiscall iparser::parse< class std::basic_string < char,struct std::char_traits < char >,class std::allocator < char > > >(class std::basic_string < char,struct std::char_traits < char >,class std::allocator < char > > const &,class json::object)" blah blah mangled signature ... referenced in function _main
that's not readable, lets create more readable making substitution
using string = class std::basic_string < char,struct std::char_traits < char >,class std::allocator < char > >
now error is
"public: class json::object __thiscall iparser::parse< string >(class string const &, class json::object)"
what says in function _main making phone call function parse<string>
fellow member of class iparser
2 parameters, const reference string , json::object value.
but wait, say, did provide definition in derived class!
json::object htmlparser::parse(std::string const& data, json::object& object) { // homecoming json::object(); }
there 3 reasons why won't work intended:
the 2nd parameter passed value in base of operations class fellow member function declaration (json::object) pass reference in derived class (json::object&). since have different signature compiler views "overloaded" version of base of operations class fellow member function. if prepare first error, , declare in base of operations class 2nd parameter reference (json::object&), signatures match, linker still complain because trying phone call base of operations class fellow member function, has not been defined. have done "overridden" base of operations classparse
fellow member function, if phone call using pointer derived class htmlparser
derived class fellow member function called. if seek phone call parse
fellow member function using pointer base of operations class iparser
compiler generates phone call function (they different!) , haven't defined it. do in order create compiler phone call parse
fellow member function of derived class htmlparser::parse
when invoke using pointer base of operations class iparser
? in order need understand polymorphism , virtual inheritance. ok, say, i'll create parse
fellow member function of iparser
base of operations class pure virtual, , forcefulness each derived class provide definition. that's when you'll run 3rd problem. 'virtual' cannot specified on fellow member function templates. reason templates resolved @ compile time while virtual functions called dynamically @ runtime based on type associated instance (the pointer). one way around problem of trying utilize both generic programming (templates) , object oriented programming (inheritance) utilize pattern called type erasure, works in cases... can read more in on tension between object-oriented , generic programming in c++ , type erasure can it
c++ templates inheritance interface
No comments:
Post a Comment