首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

模板的有关问题

2012-02-14 
模板的问题源代码如下:intmain(){char*names[]{ GeorgeM.Blum , RuthB.Boland ,BillC.Johnson , D

模板的问题
源代码如下:

int   main()
{
char*   names[]   =   { "George   M.   Blum ", "Ruth   B.   Boland ",
"Bill   C.   Johnson ", "David   P.   Moses ", "Debra   S.   Rice ",
"John   A.   Smith ", "Paul   S.   Wang "};
const   int   LEN   =   sizeof(names)   /   sizeof(char*);
int   j   =   bsearch(names, "Rice ",0,LEN-1,cmpLastname);
if(j   > =   0)
cout   < <   names[j]   < <   endl;

j   =   bsearch(names, "Doe ",0,LEN-1,cmpLastname);
if(j   <   0)
cout   < <   "Doe   not   found "   < <   endl;

return   0;
}


bsearch 的原型如下:

template <typename   T1,   typename   T2>
int   bsearch(T1   arr[],
T2   key,
int   low,int   high,
int   (*   cmp)(T1,T2)
);

编译结果如下:

“void   *bsearch(const   void   *,const   void   *,size_t,size_t,int   (__cdecl   *)(const   void   *,const   void   *))”:   不能将参数   5   从“int   (__cdecl   *)(char   *,char   *)”转换为“int   (__cdecl   *)(const   void   *,const   void   *)”
                在匹配目标类型的范围内没有具有该名称的函数

请问:这怎么了???

[解决办法]
问题的原因是名字冲突,因为VC本身就有一个库函数也叫bsearch()
有两种解决方法. 把你的bsearch()函数换个名, 或者在main()函数前加上你的bsearch()的声明

热点排行