gpt4 book ai didi

c++ - 无法使用基类的方法

转载 作者:行者123 更新时间:2023-11-28 08:00:58 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
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(),它会起作用,但对我来说很难看。我该如何解决这个问题?

附言如果重要的话,我使用带有选项 std=gnu++c11 的 g++ 4.7。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com