structures and pointer help

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

This is where I got up to.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
};


1
2
3
4
5
6
7
8
9
10
11
Student::Student() : name(""){ }

Student::Student(string n) : name(n) { }

Course::Course() : name(""){ }

Course::Course(string nm) : name(nm) { }

Course::vector<Course*> courses(string n) {
    
}
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;
};
Define a structure Student with a name and a vector<Course*> of courses.
It asking you to creat two fields: name (which you have done) and courses. And possibly methods to manipulate it.
So you should have something like:
1
2
3
4
5
6
7
8
9
10
class Student{
public:
    Student();
    Student(string n);
    void addCourse(Course* course);
    const vector<const Course*> getCourses();
private:
    string name;
    vector<Course*> courses
};
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;
Topic archived. No new replies allowed.