简体   繁体   English

错误C2660:'Aba :: f':函数未使用0个参数

[英]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: 在初始化期间,您将指针从SabaAba中:

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.

相关问题 C ++错误C2660:函数没有3个参数 - C++ Error C2660: Function does not take 3 arguments 奇怪的错误c2660“函数不带1个参数” - Strange error c2660 “function does not take 1 arguments” 错误C2660:函数没有2个参数C ++ - Error C2660: function does not take 2 arguments C++ 错误C2660:&#39;MouseListener :: MousePressed&#39;:函数未采用4个参数 - error C2660: 'MouseListener::MousePressed' : function does not take 4 arguments 简短版本:错误14错误C2660:“ Player :: addSpell”:函数未采用1个参数 - Short version: Error 14 error C2660: 'Player::addSpell' : function does not take 1 arguments 错误 C2660: 'std::allocator<char> ::allocate': function 不占用 2 arguments</char> - error C2660: 'std::allocator<char>::allocate': function does not take 2 arguments C ++中的特征库给出错误C2660:&#39;Eigen :: MatrixBase <Derived> :: eigenvalues&#39;:函数不带2个参数 - eigen library in C++ gives error C2660: 'Eigen::MatrixBase<Derived>::eigenvalues' : function does not take 2 arguments 如何解决“C2660 &#39;SWidget::Construct&#39;:函数不接受 1 个参数”? - How to resolve "C2660 'SWidget::Construct': function does not take 1 arguments"? 调用可变参数模板函数会在Visual Studio 2015中给出错误C2660 - Calling a variadic template function gives error C2660 in Visual Studio 2015 错误C2660:隐藏派生类中的方法 - error C2660 : Hiding methods in a derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM