2014-03-05 23 views
0

我有一个重载函数和带重写函数的派生类的类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':对重载函数调用暧昧

谁能解释一下吗?

由于

+1

与铿锵合作。 – chris

+0

没有什么会导致这样的错误。你确定这正是给你说错误的代码吗? – Nitesh

+0

是的,我正在编译使用VS 2005 C++编译器,并得到这个错误 – atulya

回答

1

值9.9可以转换为任一整数,或浮动作为接口。因此,它是寻找模糊性,其功能调用:

可以提显式转换,如:

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); 
} 

尝试评论任何一个有趣的如果你引入了两个函数,就像你的代码一样,你会看到模糊性。

希望这会有所帮助;)。

+0

我认为值得一提的是'9.9'是一个双字面值。 –