When the compiler compiles the class User
, and gets to the MyMessageBox
line, MyMessageBox
has not yet been defined. It has no idea it exists.
You need to make sure MyMessageBox
is defined before you use it as a member. This is solved by reversing the definition order.
Now, you have a cyclic dependency. What you can do is forward declare User
so message box can still take a pointer or reference to it, without knowing the full definition:
class User; // let the compiler know such a class will exist
class MyMessageBox
{
public:
void sendMessage(Message *msg, User *recvr); // that's ok
Message receiveMessage();
vector<Message> *dataMessageList;
};
class User
{
public:
MyMessageBox dataMsgBox; // also ok, since it's defined
};
You cannot do this the other way around. The reason is for the compiler to know how much memory User
takes up, it needs to know the size of it's members. If you were to say:
class MyMessageBox;
class User
{
public:
MyMessageBox dataMsgBox; // size not available! it's an incomplete type
};
It wouldn't work, since it doesn't know the size yet. (Because it has no full definition.)
On a side note, this function:
void sendMessage(Message *msg, User *recvr);
Probably shouldn't take either of those by pointer. You can't send a message without a message, nor can you send a message without a user to send it to. And both of those situations come true if you pass null to the function.
Rather, use a reference (possibly const):
void sendMessage(const Message& msg, User& recvr);
So it's both required to give both parameters, and easier to use.