compiler cannot recognize my class in c++ – cyclic dependency

As i said in the comment, the problem is due to cyclic dependency. In particular, your

Student.hpp includes –> Grad.hpp which in turn includes –> Core.hpp which finally includes –> Student.hpp

So as you can see from above, you ended up where you started, namely at Student.hpp. This is why it is called cyclic dependency.

To solve this just remove the #include <c3/school/Student.hpp> from Core.hpp. This is because for the friend declaration friend class Student, you don’t need to forward declare or include the Student class.

So the modified/correct Core.hpp file looks like this:

#ifndef C3_CORE_HPP
#define C3_CORE_HPP

#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
//note i have removed the include header from here

class Core {
  //other members here as before

private:
    Str name;
    double midterm{}, final{};

    friend class Student;//THIS WORKS WITHOUT INCLUDING Student.hpp
};

std::istream &read_hw(std::istream &in, Vec<double> &hws);

#endif //C3_CORE_HPP 

Leave a Comment