简体   繁体   English

覆盖关键字在编译时抛出错误

[英]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 当我编译g++ -std=c++0x ,我得到错误,突出显示重写函数, error: expected ';' at end of member declaration error: expected ';' at end of member declaration error: 'override' does not name a type error: expected ';' at end of member declaration error: 'override' does not name a type error: expected ';' at end of member declaration error: 'override' does not name a type

g++ version is 4.6. g ++版本是4.6。

override keyword is not supported by GCC 4.6. GCC 4.6不支持override关键字。 If you want to override myfunc, just delete override keyword or upgrade GCC to 4.7 version. 如果要覆盖myfunc,只需删除override关键字或将GCC升级到4.7版本。 (Ref: https://blogs.oracle.com/pcarlini/entry/c_11_tidbits_explicit_overrides ) (参考: https//blogs.oracle.com/pcarlini/entry/c_11_tidbits_explicit_overrides

g++ 4.6.3 doesn't support the override feature of C++11. g ++ 4.6.3不支持C ++ 11的override功能。 When you take away the syntatical errors, this code compiles fine in 4.7.2 and Clang. 当你拿走合成错误时,这段代码在4.7.2和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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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