0

I'm defining a class named Roster below. I've already defined the class Student. I'm trying to create an array of pointers of type Student in the Roster class to add the students to the Roster. I'm super new to programming and C++, and I'm trying to figure out why this is giving me errors. The IDE is saying it expects a ; before the * and it's missing a type specifier, which doesn't make sense to me. Thanks in advance for any help!

class Roster {
private:
    int studentIndex = -1;
    const static int numStudents = 5;
    Student* classRosterArray[numStudents];
public:
    void parse(string row);
John3136
  • 28,809
  • 4
  • 51
  • 69
Julie
  • 1
  • 3
    Apparently, class `Student` is not in fact declared before the code shown, so the compiler doesn't know what the name `Student` means. – Igor Tandetnik Jan 19 '21 at 01:25
  • 1
    Is Student included or in the same file as Roster? – Lazar Đorđević Jan 19 '21 at 01:26
  • If `Student` and `Roster` are defined in separate files that mutually (directly or indirectly) include the other, then you will require a forward declaration. In short, writing `class Student;` above the class declaration for `Roster`. When the interface (header file) only uses references or pointers to a class, this is good practice anyway. You still need to include the appropriate header in your .cpp file. – paddy Jan 19 '21 at 01:28
  • 2
    "I'm super new to programming and C++" then you should not use array of raw pointers, but better `std::vector` of smart pointers. For example I bet you already violating the rule of 3 - https://en.cppreference.com/w/cpp/language/rule_of_three – Slava Jan 19 '21 at 01:31
  • The student class is in a separate header file, which I've included at the top of this file – Julie Jan 19 '21 at 01:39
  • 1
    Sounds like my crystal ball was correct, then. Please read my comment again. It looks like you have a circular dependency in your headers. – paddy Jan 19 '21 at 01:42

0 Answers0