C++ sortId:找不到标识符
我试着按名字、年级和身份证对学生进行分类。 当我运行它时,它会给我一个错误“找不到标识符” 我有点困惑,因为我在main之前的struct中有void sortsID。 这是在我的主要,其中包括我的日期。我只包含了一些我认为在这个问题中很重要的代码 main.cppC++ sortId:找不到标识符,c++,sorting,C++,Sorting,我试着按名字、年级和身份证对学生进行分类。 当我运行它时,它会给我一个错误“找不到标识符” 我有点困惑,因为我在main之前的struct中有void sortsID。 这是在我的主要,其中包括我的日期。我只包含了一些我认为在这个问题中很重要的代码 main.cpp struct studentData { int id; string name; myDate birthday; int grade; void sortID(studentData, int); void sortGrade(
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 BDAY\n";
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;
}
}
}
}
struct studentData{
int-id;
字符串名;
我的生日;
国际等级;
void sortID(学生数据,int);
void排序级别(studentData,int);
};
int main(){
学生数据myClass[10];
myClass[0]。name=“诸如此类”;
//向类添加名称
//每个学生的随机身份证号码、B日、年级
对于(int i=0;i<10;i++){
int randomId=(rand()%(9999-1000+1))+1000;
int random grade=(rand()%(100-50+1))+50;
int random month=rand()%(12-1)+1;
int randomDay=rand()%(31-1)+1;
年份=兰特()%(1994-1990)+1990年;
myDate randomBday(随机月、随机日、随机年);
myClass[i].id=randomId;
myClass[i]。等级=随机等级;
myClass[i]。生日=随机日期;
}
studentData*idSort[10];
案例2:
cout您将functionsortID
声明为类studentData
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
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;
}
}
}
}
但你把它称为非成员函数
sortID(myClass, 10);
当然,编译器不知道这个名称是如何声明的,因为您声明了这个名称
studentData::sortID
此外,在main之后定义的名为sortID
的函数与成员函数不同,因为其第一个参数的类型为studentData s[]
,而早期声明的函数的第一个参数的类型为studentData
struct studentData {
int id;
string name;
myDate birthday;
int grade;
void sortID(studentData, int);
void sortGrade(studentData, int);
};
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 sortID(studentData s[],int n){
studentData temp;//用于交换记录的局部变量
对于(int i=0;i谢谢你们的帮助!
我刚才用了#include
所以我只是在studentData和main()之间添加了这个
struct按id比较学生{
bool运算符()(const studentData&lhs,const studentData&rhs){
返回lhs.id
包括
std::sort(myClass,myClass+10,按id()比较学生);
基本上。sortID
是一个成员函数。你需要对一个对象调用它。你可能也不想使用冒泡排序。除了需要像@jrok所说的那样对对象调用它之外,它还应该被定义为一个成员函数,而不是一个独立函数。我想你需要。更正。你声明了一个成员函数rtID
位于studentData
内部,但您从未定义过它。后来在main
之后,您定义了自由函数sortID
,但该函数在main
之前和之后都不可见。使用符号之前需要声明。现在,请决定,您想要成员函数还是自由函数?我接受这是为学术界准备的,使用std::sort
和一些函子是不可能的。