私はこのようなclass_student.h
で定義されたクラスの学生、持っている:私は(クラスファイルが含まれて)別のヘッダファイルで定義されたList List<student^>^ students = gcnew List<student^>();
を持っての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;
}
};
を。このリストには、クラスの学生のいくつかのインスタンスが追加されています。
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を見たことがあると思います。 –
ComparrをSort()メソッドに渡す方法については、[MSDN記事](https://msdn.microsoft.com/en-us/library/234b841s(v=vs.100).aspx)を参照してください。 。 dbaseエンジンに並べ替えをさせ、クエリにORDER BY句を含めることを検討してください。 –