c++ - Warning: direct base class inaccessible in derived due to ambiguity; is this serious? -
i don't understand ambiguity here. did identify line causes ambiguity , marked it.
#include <string> #include <unordered_map> class spell { protected: struct exemplar {}; spell() = default; spell (exemplar, const std::string&); }; class spellfromscroll : virtual public spell { private: static std::unordered_map<std::string, spellfromscroll*> prototypesmap; public: static void insertinprototypesmap (const std::string& tag, spellfromscroll* spell) { prototypesmap.emplace (tag, spell); } template <typename t> static spellfromscroll* createfromspell (t*); }; std::unordered_map<std::string, spellfromscroll*> spellfromscroll::prototypesmap; class spellwithtargets : virtual public spell {}; // *** note: virtual class sleep : public spellwithtargets { private: static const sleep prototype; public: static std::string spellname() {return "sleep";} private: sleep (exemplar e) : spell (e, spellname()) {} }; const sleep sleep::prototype (exemplar{}); template <typename t> class scrollspell : /*virtual*/ public t, public spellfromscroll {}; spell::spell (exemplar, const std::string& spellname) { // ambiguity warning! spellfromscroll::insertinprototypesmap (spellname, spellfromscroll::createfromspell(this)); } template <typename t> spellfromscroll* spellfromscroll::createfromspell (t*) { return new scrollspell<t>; } int main() {} /* c:\adandd>g++ -std=c++14 ambiguity.cpp -o a.exe -wall -wextra -pedantic-errors ambiguity.cpp: in instantiation of 'class scrollspell<spell>': ambiguity.cpp:32:13: required 'static spellfromscroll* spellfromscroll::createfromspell(t*) [with t = spell]' ambiguity.cpp:27:90: required here ambiguity.cpp:23:7: warning: direct base 'spell' inaccessible in 'scrollspell<spell>' due ambiguity class scrollspell : public t, public spellfromscroll {}; ^ ambiguity.cpp:23:7: warning: virtual base 'spell' inaccessible in 'scrollspell<spell>' due ambiguity [-wextra] c:\adandd> */
how serious it, , can go wrong later on program evolves?
update: solution found letting t virtual base of scrollspell<t>
. in program t derived class of spell, , spell virtual base of t. see diagram below.
spell / \ v / \ v / \ / \ spellfromscroll spellwithtargets \ \ \ \ \ sleep \ / \ / v \ / scrollspell<sleep>
in above diagram, why sleep
being virtual base of scrollspell<sleep>
solve problem?
template <typename t> class scrollspell : public t, public spellfromscroll {};
here, t = spell
, scrollspell
class has spell
class direct, non-virtual base class, , virtual base class through spellfromscroll
. ambiguity. declaring base class t
virtual might solve problem.
also don't understand point behind design, might introduce new issues.
Comments
Post a Comment