[英]error C2660: 'Aba::f' : function does not take 0 arguments
I want to know why there is an error in the following function: 我想知道为什么以下功能出错:
#include<iostream>
using namespace std;
class Saba
{
public:
Saba(){ cout << "Saba Ctor" << endl; }
Saba(const Saba& a){ cout << "Saba Copy Ctor" << endl; }
~Saba(){ cout << "Saba Dtor" << endl; }
virtual void f(){ cout << "Saba f()" << endl; }
virtual void g(){ cout << "Saba g()" << endl; }
void h(){ cout << "Saba h()" << endl; }
};
class Aba : public Saba
{
public:
Aba(){ cout << "Aba Ctor" << endl; }
Aba(const Aba& a){ cout << "Aba Copy Ctor" << endl; }
~Aba(){ cout << "Aba Dtor" << endl; }
virtual void g(){ cout << "Aba g()" << endl; }
virtual void f(int){ cout << "Aba f(int)" << endl; }
virtual void h(){ cout << "Aba h()" << endl; }
};
class Son : public Aba
{
public:
Son(){ cout << "Son Ctor" << endl; }
Son(const Son& a){ cout << "Son Copy Ctor" << endl; }
~Son(){ cout << "Son Dtor" << endl; }
void f(){ cout << "Son f()" << endl; }
void h(){ cout << "Son h()" << endl; }
};
int main()
{
Saba* sabaPtr = new Son();
Aba* abaPtr =dynamic_cast<Aba*>(sabaPtr);
abaPtr->f();
abaPtr->g();
abaPtr->h();
delete sabaPtr;
return 0;
}
I get an error " error C2660: 'Aba::f' : function does not take 0 arguments". 我收到一个错误“错误C2660:'Aba :: f':函数未采用0个参数”。 But "aba" inherited from "saba" so he can use f() of "saba"
但是“ aba”继承自“ saba”,因此他可以使用“ saba”的f()
Aba::f
, which takes 1 parameter, hides the Saba::f
. Aba::f
1个参数的Aba::f
隐藏了Saba::f
。 That is why when you call Aba::f
with no argument the compiler complains. 这就是为什么当您不带任何参数调用
Aba::f
,编译器会抱怨的原因。 Just for your information clang gave me those warnings : 仅供参考,叮当声给了我这些警告:
main.cpp:38:18: warning: 'Aba::f' hides overloaded virtual function [-Woverloaded-virtual]
main.cpp:38:18:警告:“ Aba :: f”隐藏了重载的虚拟函数[-Woverloaded-virtual]
virtual void f(int){ cout << "Aba f(int)" << endl; }
^
main.cpp:38:18: note: hidden overloaded virtual function 'Aba::f' declared here: different number of parameters (1 vs 0)
main.cpp:38:18:注意:此处声明了隐藏的重载虚拟函数'Aba :: f':参数数量不同(1 vs 0)
virtual void f(int){ cout << "Aba f(int)" << endl; }
I don't know what did you expect while writing your code but it works correctly. 我不知道您在编写代码时期望什么,但是它可以正常工作。
During initialization you've casted pointer from Saba
to Aba
in: 在初始化期间,您将指针从
Saba
到Aba
中:
Aba* abaPtr = dynamic_cast<Aba*>(sabaPtr);
So, abaPtr
is proper Aba
object (of course if dynamic cast was successfull). 因此,
abaPtr
是正确的Aba
对象(当然,如果动态abaPtr
成功)。 Next, in the line 接下来,在行中
abaPtr->f();
you are calling Aba::f(int)
which expects 1 argument to specified while you don't specify it. 您正在调用
Aba::f(int)
,它期望在不指定参数的情况下指定1个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.