Please consider the following three simplified files:
student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include "course.h"
class Student
{
private:
Course someCourse;
};
#endif
course.h:
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
private:
Student someStudent;
};
#endif
and main.cpp:
#include "student.h"
int main();
This wouldn't compile giving me
error C2146: syntax error : missing ';' before identifier 'someStudent'
It would produce a lot more errors (even for the correct portions of code) in a more complicated program. I guess the design is wrong: Student
includes Course
and Course
includes Student
. What I want to represent with it is that a student takes several courses and a course has has several students (I use vectors in a full program, avoided them here for simplicity). Any advice how this would be possible?
Thanks in advance, Vlad.
UPDATE:
Thanks for fast replies. Forward declaration of Student
class in Course
class (and removing #include "student.h"
) seems to do the job.
Sorry, I thought it wouldn't matter here, but in fact I am using vectors of const pointers in each of them (since a Student shouldn't be able to control a Course
and a Course
shouldn't be able to control a Student
), as:
vector<const Student* const> students; // in Course class