我有一个带有重载函数的类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转换为接口。因此,发现要调用哪个函数存在歧义:
您可以提及显式转换,例如:
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);
}
尝试注释上面的任何一个函数,它将完美地工作,并且,如果您在代码中同时引入了这两个函数,则会发现模棱两可。
希望这个对你有帮助 ;)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句