Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 自定义类的Visual C++排序列表_Sorting_Visual C++_C++ Cli - Fatal编程技术网

Sorting 自定义类的Visual C++排序列表

Sorting 自定义类的Visual C++排序列表,sorting,visual-c++,c++-cli,Sorting,Visual C++,C++ Cli,我有一个班级学生,在班级学生中定义。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;

我有一个班级学生,在班级学生中定义。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;
    }

};
我有一个列表^students=gcnewlist;在包含类文件的其他头文件中定义。此列表中添加了几个班级学生的实例


我想用students->Sort或者类似的方法来对这个列表进行排序。我试图重写操作符

,因为注释者指出,这是C++ + CLI,不是C++。如果您想编写C++,我建议您创建一个实际的C++项目。如果您想编写托管代码,我建议您创建一个C项目。C++/CLI是一种在C语言和C++语言之间的桥梁,一般不适用于主应用程序开发。 您需要实现接口,而不是实现操作符

在.Net中,不能在接口中指定运算符,因此IComparable接口指定方法CompareToT,如果this

一致性和最佳实践,你也应该重写等值和哈希代码,并且实现IEQuaTe.

这不是C++,但是一些方言C++或CLI可能是。请相应地重新标记并使用链接。@BaummitAugen谢谢。我对C++和VisualStudio很陌生,所以我不知道。我换了标签。我以前在做这项工作时见过CLI,所以我相信就是这样。请考虑让dBASE引擎进行排序,在查询中包含一个ORDY BY子句。
#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 definition as shown at the top]

bool operator<(student^ s1, student^ s2);
#include "class_student.h"

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