I read a program which defines two classes in two header files as below:
file IM.h:
class SM;
class IM
{
public:
void print_IM() const {
std::cout << "IM called" << std::endl;
}
void print_SM(const SM& s) const {
s.print_SM();
}
};
file SM.h:
class IM;
class SM
{
public:
SM(IM* p_) :p(p_) {};
void print_SM() const {
std::cout << "SM called" << std::endl;
}
void print_IM() const {
p->print_IM();
}
private:
IM* p = NULL;
};
The IM class has a function "print_SM" which takes SM as an argument, while SM class has a pointer to IM as a private data member.
This program doesn't compile on my machine (VS2019). The error message says "used undefined class SM". I'm also very confused about this "cross definition", where one contains the other. But a similar program comes from a library of my professor...
Is this a valid program? any way to improve it to satisfy the attempt ?