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?