没有匹配的函数来调用
No matching function for to call
我是继承的新手。
#include<iostream>
using namespace std;
class base
{ public:
void show()
{
//do something here
}
};
class derive:public base
{ public:
void show(int n,int m)
{
//do something
}};
int main()
{
derive D;
D.show();
D.show(4,5);
}
因此,编译器给出的错误是:调用 'derive::show(( 时没有匹配函数
编译器处理时
D.show();
它首先检查名称show
是否存在于derive
中。如果是,则不会在基类中查找名称。之后,它会尝试为其找到最佳匹配项。在这种情况下,没有匹配,因为 derive
中唯一名为 show
的函数需要两个类型 int
的参数。因此
D.show();
无效。
如果你想让base::show
在derive
中作为重载可用,你必须让编译器知道。
class derive : public base
{
public:
// Let the functions named show from base be available
// as function overloads.
using base::show;
void show(int n,int m)
{
cout<<"Derive Version.."<<n<<","<<m<<endl;
}
};
现在,您可以使用:
int main()
{
derive D;
D.show();
D.show(4,5);
}
编译器是正确的。在derive
类中没有函数show()
。我猜你想要的是访问base
类函数。为此,必须指定它是基类,而不是派生类。为此,您需要做的就是将范围限定为基类,执行以下操作:
derive D;
D.base::show(); //Uses the bases function
D.derive::show(4,5); // Uses the derived function
D.show(4,5); // Uses the derived function
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销