gpt4 book ai didi

c++ - 以下代码有什么问题?它没有编译

转载 作者:可可西里 更新时间:2023-11-01 18:34:48 24 4
gpt4 key购买 nike

#include <iostream> 
#include <vector>
using namespace std;

class Base
{
public:
void Display( void )
{
cout<<"Base display"<<endl;
}

int Display( int a )
{
cout<<"Base int display"<<endl;
return 0;
}

};

class Derived : public Base
{
public:

void Display( void )
{
cout<<"Derived display"<<endl;
}
};


void main()
{
Derived obj;
obj.Display();
obj.Display( 10 );
}

$test1.cpp: In function ‘int main()’:  
test1.cpp:35: error: no matching function for call to ‘Derived::Display(int)’
test1.cpp:24: note: candidates are: void Derived::Display()

在注释掉 obj.Display(10) 时,它起作用了。

最佳答案

您需要使用using 声明。 X 类中名为 f 的成员函数 隐藏 所有其他名为 f 的成员在 X 的基类中。

为什么?

阅读this explanation通过安德烈 T

您可以使用 using 声明引入那些隐藏的名称:

using Base::Display 是您需要在派生类中包含的内容。

此外 void main() 是非标准的。使用 int main()

关于c++ - 以下代码有什么问题?它没有编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5336383/

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