的排序列表我有一個類的學生,在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;
}
這不是C++,而是一些方言(C++/CLI可能?)。請相應地重新標記(使用[編輯]鏈接)。 –
@BaummitAugen謝謝。我對C++和visual studio很新,所以我不知道。我改變了標籤。我以前見過CLI,所以我相信就是這樣。 –
如何將比較器傳遞給Sort()方法在[MSDN文章](https://msdn.microsoft.com/en-us/library/234b841s(v = vs.100).aspx)中有很好的描述, 。請考慮讓dbase引擎進行排序,在查詢中包含ORDER BY子句。 –