模板函数具体化时选择模板的问题。
#include <iostream>
using namespace std;
template <typename T>
void ShowArray(T arr[],int n);
template <typename T>
void ShowArray(T * arr[],int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
int things[6]={13,31,103,301,310,130};
struct 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 < < "Listing Mr.E`s counts of things:\n ";
ShowArray(things,6);
cout < < "Listing 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;
}
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
E:\codes\test\test.cpp(1209) : error C2667: 'ShowArray ' : none of 2 overload have a best conversion
E:\codes\test\test.cpp(1209) : error C2668: 'ShowArray ' : ambiguous call to overloaded function
执行 cl.exe 时出错.
test.exe - 1 error(s), 0 warning(s)
请问为什么会这样。char * pd[3]这和第二个模板应该是完全匹配的啊。
[解决办法]
你用的VC6吧。
[解决办法]
VC6对模板支持是出了名的差。
[解决办法]
Listing Mr.E`s counts of things:
template A
13 31 103 301 310 130
Listing Mr.E`s debts:
template B
2400 1300 1800
用GCC编译运行的结果,程序没问题,看来还是环境的问题,哎,VC也太晕了.