C++ 无法显示 class 数据类型数组

C++ Can not manage to display the class datatype array

这是我写的代码,初学者

  #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 的私有部分,如下所示:

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 对象的数据成员,你最终会得到无休止的递归分配。

要完成你想要的,你需要让 _Sstudent_counteraddStudent()display() 成为 [=] 的 static 成员16=]class,例如:

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();
}

Online Demo