可能重複:
overloaded functions are hidden in derived class無法使用基類的方法
看來我不能直接使用在派生類從基類的方法是當它們存在在兩個重載基類和C++中的派生類。以下代碼會產生錯誤no matching function for call to ‘Derived::getTwo()’
。
class Base {
public:
int getTwo() {
return 2;
}
int getTwo(int, int) {
return 2;
}
};
class Derived : public Base {
public:
int getValue() {
// no matching function for call to ‘Derived::getTwo()’
return getTwo();
}
int getTwo(int) {
return 2;
}
};
如果我改變return getTwo();
到return ((Base*) this)->getTwo()
,它的工作原理,但是這看起來醜陋給我。我怎樣才能解決這個問題?
P.S.如果有問題,我使用g ++ 4.7和std = gnu ++ c11選項。
這將肯定會作爲重複關閉,但同時,快速的答案是在類範圍中將'Base :: getTwo'添加到您的Derived'定義中。 – ildjarn 2012-07-14 00:26:45