c++,template问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 14:17:29
#include<iostream.h>
#include<string.h>

template<class Type>
Type max(Type d1,type d2)
{
if(d1>d2)return d1;
else return d2;
}
int max(int,int);
float max(float,float);

void main()
{
int ival1=10,ival2=4;
float fval1=12.3,fval2=45.6;
cout<< max(ival1,ival2)<<endl;
cout<< max(fval1,fval2)<<endl;
}

为何程序说出错error C2061: syntax error : identifier 'type'
warning C4305: 'initializing' : truncation from 'const double' to 'float'
谢谢,但是改了之后:shiyan10.obj : error LNK2001: unresolved external symbol "float __cdecl max(float,float)" (?max@@YAMMM@Z)
shiyan10.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z)
Debug/shiyan10.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.

#include<iostream.h>
#include<string.h>

template<class Type>
Type max(Type d1,type d2) // !
{
if(d1>d2)return d1;
else return d2;
}
int max(int,int); // 既然定义了模板为什么还要这句
float max(float,float); // 这句也是

void main()
{
int ival1=10,ival2=4;
float fval1=12.3,fval2=45.6;
cout<< max(ival1,ival2)<<endl;
cout<< max(fval1,fval2)<<endl;
}

Type max(Type d1,type d2) //这行 Type max(Type d1,Type d2) 大小写
{
if(d1>d2)return d1;
else return d2;
}

float fval1=12.3,fval2=45.6;
//这行改成 float fval1=(float)12.3,fval2=(float)45.6;

试试看

Type max(Type d1,type d2) 大小写
type 应该为Type