c++ - how do I allocate a pointer to a class with multiple inheritance -
suppose have:
class human { string choice; public: human(string); }; class computer { string compchoice; public: computer(string); }; class refree : public human, public computer { public: string findwinner(); }; int main() { human* h1 = new human("name"); computer* c1 = new computer("ai"); refree* r1 = new refree(); }
this code fails compile with:
in function 'int main()': error: use of deleted function 'refree::refree()' note: 'refree::refree()' implicitly deleted because default definition ill-formed: error: no matching function call 'human::human()' note: candidates are: note: human::human(std::string) note: candidate expects 1 argument, 0 provided
why fail, , how can construct pointer refree
?
since human
, computer
have user-declared constructors take argument, default constructors implicitly deleted. in order construct them, need give them argument.
however, trying construct refree
without arguments - implicitly tries construct of bases without arguments. impossible. setting aside whether or not makes sense both human
, computer
, @ least have like:
refree() : human("human name") , computer("computer name") { }
more likely, want provide constructor takes 1 or both names, e.g.:
refree(const std::string& human, const std::string& computer) : human(human) , computer(computer) { }
Comments
Post a Comment