gpt4 book ai didi

c++ - 对基类中的重载函数的调用不明确

转载 作者:行者123 更新时间:2023-11-28 07:02:49 25 4
gpt4 key购买 nike

我有一个具有重载函数的类 A 和具有重写函数的派生类。

class A
{
public:
virtual void func1(float)
{
cout<<"A::func1(float) "<< endl;
}
void func1(int)
{
cout<<"A::func1(int) "<< endl;
}
};

class B: public A
{
public:
//using A::func1;

void func1(float)
{
cout << " B::Func1(float)" << endl;
}
};

int main()
{
B obj;
A obj1;

obj.func1(10);
obj1.func1(9.9); // Getting error in call

return 0;
}

错误 C2668:“A::func1”:对重载函数的调用不明确

谁能解释一下?

谢谢

最佳答案

值 9.9 可以转换为整数或 float 作为您的接口(interface)。因此,它发现调用哪个函数存在歧义:

您可以提及显式转换,例如:

obj1.func1((float)9.9);   

obj1.func1((int)9.9)

考虑下面的简单测试代码:

#include <iostream>

using namespace std;


void test(int a)
{
cout <<" integer "<<a<<endl;
};


void test(float a)
{
cout <<"Float "<<a<<endl;
}

int main ()
{
test(10.3);
}

尝试注释上面的任何一个函数,它会完美地工作,如果你引入这两个函数,就像在你的代码中一样,你会看到歧义。

希望这对您有所帮助;)。

关于c++ - 对基类中的重载函数的调用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188613/

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