简体   繁体   中英

Override keyword throwing an error while compile

I have base class

template<typename T>
Class Base {
  Base();

public:
  virtual void myfunc()=0; 
}

I have derived class

template<typename T>
Class Derived: public Base<T> {
  Derived():Base() {
  }

public:
  void myfunc() override; 
}

When I compile g++ -std=c++0x , I get the error with the overriding function highlighted, error: expected ';' at end of member declaration error: expected ';' at end of member declaration error: 'override' does not name a type

g++ version is 4.6.

override keyword is not supported by GCC 4.6. If you want to override myfunc, just delete override keyword or upgrade GCC to 4.7 version. (Ref: https://blogs.oracle.com/pcarlini/entry/c_11_tidbits_explicit_overrides )

g++ 4.6.3 doesn't support the override feature of C++11. When you take away the syntatical errors, this code compiles fine in 4.7.2 and Clang.

Moreover, I think this is what you meant your code to be:

template <typename T>
class Base {
   Base();

   public:
      virtual void myfunc() = 0; 
};

template <typename T>
class Derived : public Base<T> {
   Derived() : Base<T>() {}

   public:
      void myfunc() override;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM