gpt4 book ai didi

c++ - 错误 C2660 : 'Aba::f' : function does not take 0 arguments

转载 作者:行者123 更新时间:2023-11-28 06:09:46 24 4
gpt4 key购买 nike

我想知道为什么下面的函数会出错:

#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;

}

我收到错误消息“error C2660: 'Aba::f' : function does not take 0 arguments”。但是“aba”继承自“saba”所以他可以使用“saba”的f()

最佳答案

Aba::f,它带有 1 个参数,隐藏了 Saba::f。这就是为什么当您不带参数调用 Aba::f 时编译器会报错。仅供引用,clang 给了我这些警告:

main.cpp:38:18: warning: 'Aba::f' hides overloaded virtual function [-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)

virtual void f(int){ cout << "Aba f(int)" << endl; }

关于c++ - 错误 C2660 : 'Aba::f' : function does not take 0 arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520251/

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