ambiguous call to overloaded function

This is a discussion on ambiguous call to overloaded function within the C++ Programming forums, part of the General Programming Boards category; This code is an example from a C++ book, using overloaded template function. One template is for normal arrays and ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Question ambiguous call to overloaded function

    This code is an example from a C++ book, using overloaded template function. One template is for normal arrays and another one is for arrays to pointers. According to the book the compiler will find out which template to use, but I get two fault codes:

    error C2667: 'ShowArray' : none of 2 overload have a best conversion
    error C2668: 'ShowArray' : ambiguous call to overloaded function

    Code:
    #include <iostream>
    using namespace std;
    
    template <typename T>				//template A
    void	ShowArray(T arr[], int n);
    
    template <typename T>				//template B
    void	ShowArray(T *arr[], int n);
    
    struct	debts {
    	char	name[50];
    	double	amount;
    };
    
    int		main(void)
    {
    	int		things[6] = {13, 31, 103, 301, 310, 130};
    	debts mr_E[3] = {
    		{"Ima Wolfe", 2400.0},
    		{"Ura Foxe", 1300.0},
    		{"Iby Stout", 1800.0}
    	};
    	double	*pd[3];
    
    	for (int i = 0; i < 3; ++i)
    		pd[i] = &mr_E[i].amount;
    
    	cout << "Showing Mr. E's things:\n";
    	ShowArray(things, 6);
    	cout << "Showing Mr. E's debts:\n";
    	ShowArray(pd, 3);
    	return 0;
    }
    
    template <typename T>
    void	ShowArray(T arr[], int n)
    {
    	cout << "template A\n";
    	for (int i = 0; i < n; ++i)
    		cout << arr[i] << ' ';
    	cout << endl;
    }
    
    template <typename T>
    void	ShowArray(T *arr[], int n)
    {
    	cout << "template B\n";
    	for (int i = 0; i < n; ++i)
    		cout << *arr[i] << ' ';
    	cout << endl;
    }
    I'm using MSVC 6.
    Why isn't this working?
    Please, someone who can explain this?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ShowArray is a template and needs template parameters:

    ShowArray<double>(pd, 3);

    for instance
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    Ok, it works with the template parameter, but I still don't get it.

    I don't use template parameter when calling the function with int data ( ShowArray(things, 6); ) and this works anyway.

    According to the book the compiler will find out which instance to make from the type of data used in the arguments. The ShowArray(pd, 3); call will fit both templates but template B is more specialized so the compiler will choose it, it says.

    Why will the <double> parameter make the compiler to choose template B and not A? This confuses me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 06:51 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21