2017-01-15 26 views
0

的排序列表我有一个类的学生,在class_student.h定义如下:VISUAL C++定义类

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

}; 

我在另一头文件中定义一个列表List<student^>^ students = gcnew List<student^>();(与包括类文件)。该列表中添加了几个班学生的实例。

我想用students->Sort()或类似的东西来排序这个列表。我尝试覆盖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; 
} 
+1

这不是C++,而是一些方言(C++/CLI可能?)。请相应地重新标记(使用[编辑]链接)。 –

+0

@BaummitAugen谢谢。我对C++和visual studio很新,所以我不知道。我改变了标签。我以前见过CLI,所以我相信就是这样。 –

+0

如何将比较器传递给Sort()方法在[MSDN文章](https://msdn.microsoft.com/en-us/library/234b841s(v = vs.100).aspx)中有很好的描述, 。请考虑让dbase引擎进行排序,在查询中包含ORDER BY子句。 –

回答

0

作为评论者指出,这是C++/CLI,而不是C++。如果你想编写C++,我建议你创建一个真正的C++项目。如果你想编写托管代码,我建议你创建一个C#项目。 C++/CLI旨在用作托管语言(如C#)和C++之间的桥梁,并且它通常不应用于主应用程序开发。

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

在.NET中,运营商不能在一个指定的接口,因此IComparable<T>接口指定方法CompareTo(T),它返回-1如果this < other0如果相等,并1如果更大。

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