+ Reply to Thread
Results 1 to 2 of 2

Thread: Why below code fails with "ambiguous call to overloaded function"?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    27

    Why below code fails with "ambiguous call to overloaded function"?

    I have below code:

    Code:
    class MyFunction
    {
    public:
    	void MyFunction1(int i)
    	{
    		char message[100] ;
    		sprintf(message, "value is : %d\n", i) ;
    		OutputDebugString(message) ;
    	}
    
    	void MyFunction1(float f)
    	{
    		char message[100] ;
    		sprintf(message, "value is : %f\n", f) ;
    		OutputDebugString(message) ;
    	}
    } ;
    
    int main()
    {
    	MyFunction objMyFunction ;
    	objMyFunction.MyFunction1(1.1) ;
    
    	return 0 ;
    }
    It raises an error: error C2668: 'MyFunction1' : ambiguous call to overloaded function

    Why?

    How can I resolve this issue?

  2. #2
    You are getting the error because compiler cannot make decision what method of the class it should execute.

    You can solve the issue by using one of the following methods:

    Code:
    int main()
    {
    	MyFunction objMyFunction ;
    	objMyFunction.MyFunction1((float)1.1) ;
    
    	objMyFunction.MyFunction1(1.1f) ;
    
    	return 0 ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts