I need help writing a code for my homework, how would I write this structure.
Define a structure Student with a name and a vector<Course*> of courses.
Define a structure Course with a name and a vector<Student*> of enrolled students
Yep, you've got it. There is only one thing left out. You should forward declare Course in order to make a pointer to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Course; // You are now allowed to make a pointer to this class even though it hasn't been defined
class Student{
public:
Student();
Student(string n);
vector<Course*> courses(string n);
private:
string name;
};
class Course {
public:
Course();
Course(string nm);
vector<Student*> students(string n);
private:
string name;
};
Thanks for the response guys I was able to get my code to compile. for const vector<const Course*> getCourses();
can I also write vector<const Course*> getCourses() const;
would that be similar or not.
Not. T foo() const;
says that calling foo() will not modify the object. That is true about getCourses().
You can modify the returned value, but since it is just a copy, that is not related to the Student.
const T foo();
says that the returned object cannot be changed.
You could do both. const vector<const Course*> getCourses() const;