如何修改此函数,以便如果函数的参数是特定结构,则返回具有较大整数的结构?
How can I modify this function so that if the parameters of the function are specific structs it returns the struct with the bigger integer?
我必须修改这个max函数,如果它处理一个struct Student
,它将返回最大等级。 代码如下:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef struct Student {
char* name;
int grade;
}Student;
template<typename T>
T Max(T var1, T var2) {
if (sizeof(T) == sizeof(Student))
return(var1.grade > var2.grade) ? var1 : var2;
return (var1 > var2) ? var1 : var2;
}
int main() {
int i = 39;
int j = 20;
cout << "Max(i,j)=" << Max(i, j)<<endl;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1,f2)=" << Max(f1, f2) << endl;
string s1 = "Hello";
string s2 = "World";
cout << "Max(s1,s2)="<<Max(s1,s2) << endl;
Student first_student;
Student second_student;
first_student.name = new char[30];
second_student.name = new char[30];
strcpy(first_student.name, "Popescu David");
strcpy(second_student.name,"Gigel Petrovici");
first_student.grade = 7;
second_student.grade = 6;
cout << "Max(student1,student2)=" << Max(first_student, second_student).grade << endl;
return 0;
}
但是我得到这些错误:
1>------ Build started: Project: OOP, Configuration: Debug Win32 ------
1>LastTODO.cpp
1>c:usersdragossourcereposoopooplasttodo.cpp(11): error C2228: left of '.grade' must have class/struct/union
1>c:usersdragossourcereposoopooplasttodo.cpp(11): note: type is 'T'
1> with
1> [
1> T=int
1> ]
1>c:usersdragossourcereposoopooplasttodo.cpp(22): note: see reference to function template instantiation 'T Max<int>(T,T)' being compiled
1> with
1> [
1> T=int
1> ]
1>c:usersdragossourcereposoopooplasttodo.cpp(11): error C2039: 'grade': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>d:visual studiovctoolsmsvc14.16.27023includexstring(4373): note: see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>c:usersdragossourcereposoopooplasttodo.cpp(28): note: see reference to function template instantiation 'T Max<std::string>(T,T)' being compiled
1> with
1> [
1> T=std::string
1> ]
1>c:usersdragossourcereposoopooplasttodo.cpp(12): error C2676: binary '>': 'T' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> T=Student
1> ]
1>c:usersdragossourcereposoopooplasttodo.cpp(38): note: see reference to function template instantiation 'T Max<Student>(T,T)' being compiled
1> with
1> [
1> T=Student
1> ]
1>Done building project "OOP.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
从 C++17 开始,您可以使用if constexpr
请求以下行
return(var1.grade > var2.grade) ? var1 : var2;
仅当T
Student
时才编译:
template<typename T>
T Max(T var1, T var2) {
if constexpr ( std::is_same_v<T, Student> )
return(var1.grade > var2.grade) ? var1 : var2;
else
return (var1 > var2) ? var1 : var2;
}
演示
另一种可能性是将Max
的主要版本定义为:
template<typename T>
T Max(T var1, T var2) {
return (var1 > var2) ? var1 : var2;
}
并为Student
结构提供专用化:
template<>
Student Max<Student>(Student var1, Student var2) {
return(var1.grade > var2.grade) ? var1 : var2;
}
相关文章:
- 使用不带参数的函数访问结构元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 链表,反向函数,数据结构
- 结构向量中自定义结构函数的内存使用
- C++ 中的嵌套结构函数
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 如何将值从外部传递到结构函数
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- C++结构函数的多个定义
- C++ 另一个标头中的结构函数给出"missing type specifier"
- 如何在C中找到结构/函数的父头文件
- C++二进制文件到结构函数
- C++ 将结构函数作为继承结构中函数的参数传递
- 如何在 main 之外的类中使用结构函数
- 在类中使用结构函数,在基类 c++ 中使用变量
- C++ 结构函数体调用另一个结构函数,同一个模块
- 是存储在对象中的类/结构函数
- Eclipse CDT 在从 std::map 访问时无法解析结构函数
- C++模板自身名称作为模板模板参数在同一模板结构函数内传递
- 如何使用真正的字符串类型来表示类结构函数中的字符串