[操作疑難] C++ 呢句嘢應該點寫法 ?

本帖最後由 ghostkcleung 於 2021-5-19 01:00 編輯

class Base {
  public:
  virtual void func ( ) { }

  protected:

  void a(int x) {
    a(x, 0);
  }

  virtual void a(int x, int y) = 0;
} ;

class Dervied: public Base {
  public:
  void func ( ) {
    a ( 1 ) ;  // no matching function for call to 'Dervied::a(int)'
    // 明明個 Method 承繼咗架喎。

  }

  protected:
  void a(int x, int y) { }
} ;

Base 個 a(int x, int y) 有兩個 arguments, function signature 錯咗

TOP

但我一樣係 overload 咗 a(int x)
既然呢啲問題係無得解,
最後我係改 method name

Base 個 a(int x, int y) 有兩個 arguments, function signature 錯咗
rkkc 發表於 2021-5-19 01:22

TOP

a(int x), a(int x, int y) argument 多咗但相同 type , overload 唔到

唔同 type 例: a(double x) 就可以 overload

TOP

https://en.cppreference.com/w/cpp/language/unqualified_lookup
https://en.wikipedia.org/wiki/Dominance_(C%2B%2B)
大概嘅情況就係 Derived::a(int, int) 會遮住 Base::a 所有name lookup
重要分C++11之前同之後兩種做法...
如果無Google查可能就變"鬼故"

TOP