【发布时间】:2014-04-07 20:28:19
【问题描述】:
我有一个基类,它包含一个抽象方法func(int, float, unsigned) 和对此方法的重载func(int),以及一个实现抽象方法的派生类
.
class Base
{
public:
virtual void func(int x, float y, unsigned z) = 0;
void func(int x)
{
cout << "func with x only" << endl;
}
};
class Derived : public Base
{
public:
void func(int x, float y, unsigned z)
{
cout << "func override" << endl;
}
};
在我的代码中,我有一个派生类的实例,它调用基类func(int) 的重载方法。
int main()
{
Derived d;
d.func(10); // <<--------- 'COMPILATION ERROR'
return 0;
}
在编译这段代码时,我得到以下编译错误:
error: no matching function for call to 'Derived::func(int&)'
note: candidates are: virtual void Derived::func(int, float, unsigned int)
这个错误的原因是什么/为什么这个代码不起作用?
【问题讨论】:
标签: c++ inheritance g++ overriding overloading