How to override a pure virtual function in template class in C++? -
i trying override pure virtual function:
template <class p, class r> class visitor { public: virtual ~visitor() {} virtual r visit(astnode& n, p p) = 0; }; class tostringvisitor : public visitor<string, int> { virtual string visit(astnode& n, int p) override { return ""; } }
however, error:
error: 'virtual std::__cxx11::string tostringvisitor::visit(astnode&, int)' marked 'override', not override virtual string visit(astnode& n, int p) override {
why isn't overriding?
none of other questions find on have such succinct example, or detail of template return value in superclass.
in line:
class tostringvisitor : public visitor<string, int> { parameter type (p) --^ ^-- return type (r)
you have swap string
int
because mismatches parameter should passed (p = int
) , return type (r = string
):
class tostringvisitor : public visitor<int, string> { parameter type (p) --^ ^-- return type (r)
then conform template class declaration.
Comments
Post a Comment