【问题标题】: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?如何修改此函数,以便如果函数的参数是特定结构,它返回具有更大整数的结构?
【发布时间】:2019-10-13 10:50:53
【问题描述】:

我必须修改这个 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:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): error C2228: left of '.grade' must have class/struct/union
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): note: type is 'T'
1>        with
1>        [
1>            T=int
1>        ]
1>c:\users\dragos\source\repos\oop\oop\lasttodo.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:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): error C2039: 'grade': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>d:\visual studio\vc\tools\msvc\14.16.27023\include\xstring(4373): note: see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>c:\users\dragos\source\repos\oop\oop\lasttodo.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:\users\dragos\source\repos\oop\oop\lasttodo.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:\users\dragos\source\repos\oop\oop\lasttodo.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++ templates struct max


【解决方案1】:

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

Demo


另一种可能性是将Max 的主要版本定义为:

template<typename  T>
T Max(T var1, T var2) {
    return (var1 > var2) ? var1 : var2;
}

并为Student struct 提供专业化:

template<>
Student Max<Student>(Student var1, Student var2) {
    return(var1.grade > var2.grade) ? var1 : var2;
}

【讨论】:

  • 感谢您的详细回答。还有一件事:您能告诉我如何在 VS 2017 上启用 C++ 17 编译器吗?我知道关于这个主题有一些关于 SO 的答案,但它们似乎很难让我得到它们,我更喜欢一些“点”,更容易和更快地理解答案。
  • Project - Properties/Settings - C/C++ - Language - 在选项 C++ standard 中,您需要选择 /std:c++latest(首选)或 /std:c++17(在这种情况下 - VS17不知道是否支持)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多