C++函数模板

C++ function templates

本文关键字:函数模板 C++      更新时间:2023-10-16

我用 c++ 写下了这个程序,以使用函数模板找出三个数字中最大的一个。我不知道为什么我会收到一个错误,即 Tx、Ty 和 Tz 未在范围内声明。请帮我解决此代码。

#include <iostream>
//Program to find out the largest among the three given numbers using function template
using namespace std;
template<class T>
T greatest(Tx, Ty, Tz){
if(x>y){
    if(x>z){
            return x;
    }
    else{
        return z;
    }
}
else if(y>x){
        if(y>z){
            return y;
        }
        else{
            return z;
        }

}
}
int main()
{
int choice;
int a,b,c;
float x,y,z;
double p,q,r;
char option;
do{
    cout<<"Find the greatest numbers"<<endl;
    cout<<"1. Among integer numbers"<<endl;
    cout<<"2. Among float numbers"<<endl;
    cout<<"3. Among double numbers"<<endl;
    cin>>choice;
    switch(choice){
    case 1:
        cout<<"Enter three Integer numbers"<<endl;
        cin>>a>>b>>c;
        cout<<"The greatest integer is "greatest(a,b,c)<<endl;
    case 2:
        cout<<"Enter three floating numbers"<<endl;
        cin>>x>>y>>z;
        cout<<"The greatest float number is "greatest(a,b,c)<<endl;
    case 3:
        cout<<"Enter three double numbers"<<endl;
        cin>>p>>q>>r;
        cout<<"The greatest float number is "greatest(p,q,r)<<endl;
    }

cout<<"Do you want to continue (y/n)"<<endl;
cin>>option;
}while(option=='y' || option=='Y');
return 0;
}

替换:

T greatest(Tx, Ty, Tz){

跟:

T greatest(T x, T y, T z){