C++函数模板
C++ function templates
我用 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){
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 将重载的成员函数传递给函数模板
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 具有常量引用参数的函数模板专用化
- std::span<const T> 作为函数模板中的参数
- 如何编写一个完美的缩写函数模板?
- 仅在函数模板中为那些定义了函数的类型执行函数
- 如何在C++中伪造虚拟可变参数函数模板?
- 以下代码中的函数模板有什么问题?
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 将显式实例化的函数模板与转换匹配
- 使用定义函数模板别名
- 函数模板返回类型
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- C++ std::functional 中的可变参数函数模板
- 单行函数模板 c++ 的内联性保证
- C++函数模板需要 &for 数组参数
- 概念解析为使用 std::make_signed_t 时意外的函数模板
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板