1

I have a problem with calling an inherited method. Probably I miss some virtual, const or & but I cannot find where

I have a base class Classifier with one "real" and one virtual function, the "real" function calls the virtual one. The child class MyClassifier defines the virtual inherited methon. Now when I call the "real" class on the MyClassifier object, I get compiler error.

class Classifier {
    public:
    bool classify(const Image& ii) 
    { 
        return classify(ii, ii.getRect()); 
    }

    virtual bool classify(const Image& ii, const rect_t& rect) const = 0;
};

class MyClassifier : public Classifier {
    public:
    bool classify(const Image& ii, const rect_t& rect) const;
};

bool
MyClassifier::classify(const Image& ii, const rect_t& rect) const
{
    // do stuff...
}

The calling code is:

// main...
MyClassifier c;
Image some_image;

c.classify(some_image);

And the error:

error: no matching function for call to ‘MyClassifier::classify(const Image&) const’
note: candidate is:
note: virtual bool MyClassifier::classify(const Image&, const rect_t&) const
note:   candidate expects 2 arguments, 1 provided
2
  • @Jakub To really use virtual mechanism of C++ you'd need to use pointers. Commented Dec 20, 2011 at 8:02
  • The only thing I care is to have 1-parameter classify calling tits 2-parameter version for each inherited class. For me it can be with or without virtual. Without even better as I won't get vtable performance penalty Commented Dec 20, 2011 at 8:04

3 Answers 3

5

The overloaded method in the subclass hides the method from the base-class. You can fix it with a using-declaration:

class MyClassifier : public Classifier {
public:
    using Classifier::classify;
    bool classify(const Image& ii, const rect_t& rect) const;
};
Sign up to request clarification or add additional context in comments.

1 Comment

And for the OP, here is more detail than you probably want to know right now about name hiding: gotw.ca/publications/mill08.htm
3

You can either add a using declaration as in Björn Pollex's answer, or an explicit qualifier at the call site:

c.Classifier::classify(some_image);

Comments

0

You have to use the scope operator to specifically indicate that you are trying to call the base class version of the function and not the current class version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.