sortId:未找到标识符

sortId: identifier not found

本文关键字:标识符 sortId      更新时间:2023-10-16

我正在尝试按名字、年级和id对学生进行排序。当我运行它时,它给了我一个错误"标识符未找到"。我有点困惑,因为我在main之前的struct中有一个void sortsID。这是我的主目录,其中包括我的myDate.h。在这个问题中,我只包含了一些我认为重要的代码

main.cpp

struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
int main() {
studentData myClass[10];
myClass[0].name = "blah blah";
//adding names to class
    //random id numbers, bday, grade for each student
for (int i = 0; i < 10; i++) {
    int randomId = (rand() % (9999 - 1000 + 1)) + 1000;
    int randomGrade = (rand() % (100 - 50 + 1)) + 50;
    int randomMonth = rand() % (12 - 1) + 1;
    int randomDay = rand() % (31 - 1) + 1;
    int randomYear = rand() % (1994 - 1990) + 1990;
    myDate randomBday(randomMonth, randomDay, randomYear);
    myClass[i].id = randomId;
    myClass[i].grade = randomGrade;
    myClass[i].birthday = randomBday;
}
studentData *idSort[10];

    case 2:
        cout << "Sorting by ID" << endl;
        sortID(myClass, 10);
        cout << "Displaying original list";
        cout << "Student Info:n";
        cout << "=================================================n";
        cout << "NAME               ID#          GRADE   BDAYn";
        for (int i = 0; i < 10; i++) {
            idSort[i] = &(myClass[i]);
            cout << left << setw(18) << idSort[i]->name << " ";
            cout << left << setw(12) << idSort[i]->id << " ";
            cout << setw(11) << setprecision(4) << idSort[i]->grade << " ";
            idSort[i]->birthday.display();
            cout << " " << endl;
        }
        break;

system("PAUSE"); };

void sortID(studentData s[], int n) {
studentData temp;   // Local variable used to swap records
for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number,
    swap the records
        if (s[i].id > s[i + 1].id)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}
void sortGrade(studentData s[], int n) {
studentData temp;   // Local variable used to swap records
for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number, swap the records
        if (s[i].grade > s[i + 1].grade)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}
void sort_on_name(studentData s[], int n) {
studentData temp;   // Local variable used to swap records
for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].name is later in alphabet than s[i+1].name, swap the two records
        if (strcmp(s[i].name, s[i + 1].name) > 0)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}

"

您将函数sortID声明为类studentData的非静态成员函数

struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};

但是你把它叫做非成员函数

sortID(myClass, 10);

当然编译器不知道这个名字是怎么声明的因为你声明了name

studentData::sortID

并且在main之后定义的名称为sortID的函数与成员函数不同,因为它的第一个参数类型为studentData s[],而早先声明的函数的第一个参数类型为studentData

void sortID(studentData s[], int n) {
studentData temp;   // Local variable used to swap records
for (int i = 0; i<n; i++)
{
    for (int i = 0; i<n; i++)
    {
        // If s[i].student_number is greater than s[i+1].student_number,
    swap the records
        if (s[i].id > s[i + 1].id)
        {
            temp = s[i];
            s[i] = s[i + 1];
            s[i + 1] = temp;
        }
    }
}
}

感谢大家的帮助!
我只是用了#include <算法>

我只是在studentData和main()

之间添加了这个
struct compare_student_by_id {
     bool operator() (const studentData & lhs, const studentData & rhs) { 
     return lhs.id < rhs.id; 
}};
包括

std::sort(myClass, myClass + 10, compare_student_by_id());