Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override not allowed outside a class definition [closed]

Tags:

c++

c++17

I created an abstract class and a concrete subclass:

//bca.h
#include <string>
class bca {
public:
    virtual std::string get_virtual_year() const = 0;
};
//bca_interface.h
#include "bca.h"
class bca_interface : public bca {
public:
    std::string get_virtual_year() const override;
};
//bca_interface.cpp
#include "bca_interface.h"
std::string bca::get_virtual_year() const override {
    return "";
}

When I compile bca_interface.cpp with g++, I get:

 error: virt-specifiers in ‘get_virtual_year’ not allowed outside a class definition
 std::string bca::get_virtual_year() const override {
like image 768
user1406186 Avatar asked Oct 03 '18 12:10

user1406186


1 Answers

The error says it all:

error: virt-specifiers in ‘get_virtual_year’ not allowed outside a class definition

You cannot put a virt-specifier (override and final) outside of a class definition. You only put that specifier on the function declaration within the class definition. The same is true for, e.g., explicit, static, virtual, ...

Where you have it in the header is correct. In your source, just remove it.

like image 166
Barry Avatar answered Sep 28 '22 14:09

Barry