如何修改此函数,以便如果函数的参数是特定结构,则返回具有较大整数的结构?

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?

本文关键字:结构 函数 返回 何修改 整数 参数 如果 修改      更新时间:2023-10-16

我必须修改这个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;

仅当TStudent时才编译:

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;
}