2

So here is the base class:

class MovieRepo {
protected:
    MyLista<Movie> all;
public:
    MovieRepo();
    void store(const Movie& m);
}

And this one is the override:

class RepoFile : public MovieRepo
{
private:
    string filename;
    void loadFromFile();
    void storeToFile();

public:

    RepoFile(string _filename) : filename{ _filename } { this->loadFromFile(); };

    void store(const Movie& m) override { // here is the error
        MovieRepo::store(m);
        storeToFile();
    }
}

It says:'RepoFile::store': method with override specifier 'override' did not override any base class methods

  • 1
    The message from `clang++` is a little more specific: _error: only virtual member functions can be marked 'override'_" – Ted Lyngmo May 05 '20 at 20:31

3 Answers3

3

MovieRepo::store() is not a virtual function. The override specifier only works with virtual functions. Change the base class method to a virtual method to fix the issue.

Alex
  • 1,794
  • 9
  • 20
3

The override keyword can only be used when the function it's overriding is virtual. (It is, effectively, just a 'safer' way of specifying virtual in a derived class, so that you can spot cases where there is no matching base-class function.)

cppreference

Fix: Either remove the override keyword (the base-class function will still be overriden) or add virtual to the base-class function).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

The key to the solution is the keyword virtual.

The virtual specifier specifies that a non-static member function is virtual and supports dynamic dispatch. It may only appear in the decl-specifier-seq of the initial declaration of a non-static member function (i.e., when it is declared in the class definition).

See this for why do we need virtual functions?. With "virtual" we get "late binding". Which implementation of the method is used gets decided at run time based on the type of the pointed-to object - what it was originally constructed as (not on the type of the pointer that you call through!).

If you want such "late binding" and want to invoke the right method (store) at run-time irrespective of the type of the pointer that you use to call store(), then go ahead with virtual functions with the implementation below:

Try this:

class MovieRepo {
public:
    // ....
    virtual void store(const Movie& m);
}

class RepoFile: public MovieRepo {
public:
    void store(const Movie& m) override 
    { 
        // ....
    }
}

More details on compilation warning/error:

The override keyword serves two purposes:

  1. It shows the reader of the code that "this is a virtual method, that is overriding a virtual method of the base class."
  2. The compiler also knows that it's an override, so it can "check" that you are not altering/adding new methods that you think are overrides.

In your case, compiler is warning you that the method your virtual function (in class RepoFile) isn't overriding any virtual function in base class.

abhiarora
  • 9,743
  • 5
  • 32
  • 57