我有一个班级学生,在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;
}
};
我在另一个头文件中定义了一个列表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;
}
答案 0 :(得分:0)
正如评论者所说,这是C ++ / CLI,而不是C ++。如果你想编写C ++,我建议你创建一个实际的C ++项目。如果要编写托管代码,我建议您创建一个C#项目。 C ++ / CLI旨在充当托管语言(如C#)和C ++之间的桥梁,通常不应用于主应用程序开发。
您需要实现接口IComparable<student^>^
,而不是实现运算符。
在.Net中,不能在接口中指定运算符,因此IComparable<T>
接口指定方法CompareTo(T)
,如果-1
,则返回this < other
,{{1}如果相等,则0
如果更大。
为了保持一致性和最佳做法,您还应覆盖1
和equals
,并实施hashCode
。