visual c++ 自定义排序列表 Class

visual c++ Sort List of Custom Class

我有一个 class 学生,在 class_student.h 中定义如下:

#pragma once
using namespace System;
using System::Windows::Forms::MessageBox;
using namespace System::Collections::Generic;
using namespace MySql::Data::MySqlClient;

public ref class student {

public:
    String^ user_name;
    String^ my_class;
    int weighted_grades;
    int weighted_totals;
    float average_percent;

    int get_weighted_grades() {
        String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
        MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
        MySqlCommand^ get_grades = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_grades inner join tbl_test_instances on tbl_grades.test_instance=tbl_test_instances.id inner join tbl_students on tbl_test_instances.student_id = tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_tests.test_id where tbl_students.class = tbl_tests.class and username='" + user_name + "';", con_database);
        MySqlDataReader^ my_reader;
        int grades;
        try {
            con_database->Open();
            my_reader = get_grades->ExecuteReader();
            my_reader->Read();
            grades = my_reader->GetInt32(0);
        }
        catch (Exception^ ex) {
            MessageBox::Show(ex->Message);
        }
        return grades;
    }

    int get_weighted_totals() {
        String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
        MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
        MySqlCommand^ get_totals = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_questions inner join tbl_test_questions on tbl_questions.question_id=tbl_test_questions.question_id inner join tbl_test_instances on tbl_test_questions.test_id=tbl_test_instances.test_id inner join tbl_students on tbl_test_instances.student_id=tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_test_questions.test_id where tbl_students.class=tbl_tests.class and username='" + user_name + "';", con_database);
        MySqlDataReader^ my_reader;
        int totals;
        try {
            con_database->Open();
            my_reader = get_totals->ExecuteReader();
            my_reader->Read();
            totals = my_reader->GetInt32(0);
        }
        catch (Exception^ ex) {
            MessageBox::Show(ex->Message);
        }
        return totals;
    }

    student(String^ user_name, String^ txt_class) {
        this->user_name = user_name;
        this->my_class = txt_class;
        this->weighted_grades = get_weighted_grades();
        this->weighted_totals = get_weighted_totals();
        this->average_percent = ((float)weighted_grades / (float)weighted_totals) * 100;
    }

};

我在另一个头文件(包含 class 文件)中定义了一个列表 List<student^>^ students = gcnew List<student^>();。此列表中添加了 class 学生的多个实例。

我想使用 students->Sort() 或类似的东西来对这个列表进行排序。我尝试在 class 定义内部和外部覆盖 operator<,但是当我调用 Sort() 时,我仍然收到一条错误消息,指出无法比较项目。

在尝试通过 operator< 执行此操作时,我使用了这个,如果有帮助的话:

#pragma once
using namespace System;
using System::Windows::Forms::MessageBox;
using namespace System::Collections::Generic;
using namespace MySql::Data::MySqlClient;

public ref class student {

public:
    String^ user_name;
    String^ my_class;
    int weighted_grades;
    int weighted_totals;
    float average_percent;

    int get_weighted_grades() {
        String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
        MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
        MySqlCommand^ get_grades = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_grades inner join tbl_test_instances on tbl_grades.test_instance=tbl_test_instances.id inner join tbl_students on tbl_test_instances.student_id = tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_tests.test_id where tbl_students.class = tbl_tests.class and username='" + user_name + "';", con_database);
        MySqlDataReader^ my_reader;
        int grades;
        try {
            con_database->Open();
            my_reader = get_grades->ExecuteReader();
            my_reader->Read();
            grades = my_reader->GetInt32(0);
        }
        catch (Exception^ ex) {
            MessageBox::Show(ex->Message);
        }
        return grades;
    }

    int get_weighted_totals() {
        String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
        MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
        MySqlCommand^ get_totals = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_questions inner join tbl_test_questions on tbl_questions.question_id=tbl_test_questions.question_id inner join tbl_test_instances on tbl_test_questions.test_id=tbl_test_instances.test_id inner join tbl_students on tbl_test_instances.student_id=tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_test_questions.test_id where tbl_students.class=tbl_tests.class and username='" + user_name + "';", con_database);
        MySqlDataReader^ my_reader;
        int totals;
        try {
            con_database->Open();
            my_reader = get_totals->ExecuteReader();
            my_reader->Read();
            totals = my_reader->GetInt32(0);
        }
        catch (Exception^ ex) {
            MessageBox::Show(ex->Message);
        }
        return totals;
    }

    student(String^ user_name, String^ txt_class) {
        this->user_name = user_name;
        this->my_class = txt_class;
        this->weighted_grades = get_weighted_grades();
        this->weighted_totals = get_weighted_totals();
        this->average_percent = ((float)weighted_grades / (float)weighted_totals) * 100;
    }
    bool operator<(student^ other) {
        return this->average_percent > other->average_percent;
    }
};

或者这样:

class_student.h:

[class definition as shown at the top]

bool operator<(student^ s1, student^ s2);

class_student.cpp:

#include "class_student.h"

bool operator<(student^ s1, student^ s2) {
    return s1->average_percent > s2->average_percent;
}

正如评论者所指出的,这是 C++/CLI,而不是 C++。如果您想编写 C++,我建议您创建一个实际的 C++ 项目。如果您想编写托管代码,我建议您创建一个 C# 项目。 C++/CLI 旨在充当托管语言(如 C#)和 C++ 之间的桥梁,一般不应将其用于初级应用程序开发。

您需要实现接口 IComparable<student^>^,而不是实现运算符。

在 .Net 中,不能在接口中指定操作符,因此 IComparable<T> 接口指定方法 CompareTo(T),其中 returns -1 if this < other0 如果相等,1 如果更大。

为了保持一致性和最佳实践,您还应该覆盖 equalshashCode,并同时实施 IEquatable<T>