sortId:未找到标识符
sortId: identifier not found
我正在尝试按名字、年级和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());
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- #ifdef和未声明的标识符
- f2、f3、f4标识符未找到
- 设置 Visual Studio for MPI: 找不到标识符错误
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 如何让 GCC/Clang 在保留标识符上出错
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 使用说明符 extern 声明的C++中的标识符链接
- C++:枚举:错误:应使用标识符而不是"}"
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- Visual Studio C++ PlaySound 标识符未定义
- 为什么我们不编写可以处理C++标识符的汇编器和链接器?
- 使用未声明的标识符"sqrt"?
- 找不到标识符合并
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 在 c++ 中找不到此函数的标识符
- sortId:未找到标识符