c++无法显示类数据类型数组



这是我写的代码,完全初学者

#include <iostream>
#include <cstring>
using namespace std;

class Person
{
public:
string ID;
string name;
string surname;
string department;
string email;
public:
//get and set functions for ID, Name, Surname, Department, Email properties
string getID()
{
return this->ID;
};
void setID(string _ID)
{
this->ID = _ID;
};
string getName()
{
return this->name;
};
void setName(string _name)
{
this->name = _name;
};
string getSurname()
{
return this->surname;
};
void setSurname(string _surname)
{
this->surname = _surname;
};
string getDepartment()
{
return this->department;
};
void setDepartment(string _department)
{
this->department = _department;
};
string getEmail()
{
return this->email;
};
void setEmail(string _email)
{
this->email = _email;
};
};
//inherit Student class from Person class
class Student :public Person
{
private:
int student_counter = 0;
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add 
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student _S[100]
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
};


int
main()
{
Student stu;
stu.addStudent("ST123456", "Ege", "Inan", "CS", "ege @ gmail.com");
stu.display();
}

问题在这里

//inherit Student class from Person class
class Student :public Person
{
private:
int student_counter = 0;
public:
//constructor
Student()
{
};

Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add 
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student _S[100];
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}

//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}

}
};
};

我得到的错误信息是:

main.cpp:106:9: error: ‘_S’ was not declared in this scope
106 |   cout<<_S[i].getID()<<" - ";
|         ^~

我尝试将Student _S的定义移动到Student类的私有部分,像这样:

class Student :public Person
{
private:
int student_counter = 0;
Student _S[100];
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add 
void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
};

但是我得到了这个:

65 |   Student _S[100];
|           ^~
main.cpp:61:7: note: definition of ‘class Student’ is not complete until the closing brace
61 | class Student:public Person
|       ^~~~~~~

处理这个问题的最好方法是什么?在哪里定义数组以使程序按预期运行?

在第一个代码中,_SaddStudent()内部的局部变量,因此display()不能访问它。

您的第二个代码更好,因为display()现在可以访问_S。但是Student对象不能保存任何同时也是Student对象的数据成员,你最终会得到一个无穷无尽的递归分配。

要完成您想要的,您需要使_S,student_counter,addStudent()display()成为Student类的static成员,例如:

class Student :public Person
{
private:
static int student_counter;
static Student _S[100];
public:
//constructor
Student()
{
};
Student(string id, string Name, string Surname, string Department, string Email)
{
setID(id);
setName(Name);
setSurname(Surname);
setDepartment(Department);
setEmail(Email);
}
//student add 
static void addStudent(string id, string Name, string Surname, string Department, string Email)
{
if (student_counter >= 100)
{
cout << "cant add more students";
}
else
{
Student newS;
newS.setID(id);
newS.setName(Name);
newS.setSurname(Surname);
newS.setDepartment(Department);
newS.setEmail(Email);
_S[student_counter] = newS;
student_counter++;
}
}
//display students
static void display()
{
for (int i = 0; i < student_counter; i++)
{
cout << _S[i].getID() << " - ";
}
}
};
int Student::student_counter = 0;
Student Student::_S[100];
int main()
{
Student::addStudent("ST123456", "Ege", "Inan", "CS", "ege @ gmail.com");
Student::display();
}

在线演示

最新更新