I am using two classes in my C++ application. The code is as follows:
class MyMessageBox
{
public:
void sendMessage(Message *msg, User *recvr);
Message receiveMessage();
list<Message> dataMessageList;
};
class User
{
public:
MyMessageBox *dataMsgBox;
};
The msg is a pointer to a derived class object of Message class. I have implemented the function sendMessage as follows:
void MyMessageBox::sendMessage(Message *msg, User *recvr)
{
Message &msgRef = *msg;
recvr->dataMsgBox->dataMessageList.push_back(msgRef);
}
When I compile this code, I get the following error:
undefined reference to `vtable for Message’. Please help me out to solve this issue.
Thanks,
Rakesh.
I don’t know what you’re trying to do with that msgRef, but it’s wrong. Are you an ex-Java programmer, by any chance?
If
Messageis a base class for derivatives ofMessage, you need to store pointers in the list. Changelist<Message>tolist<Message*>; andpush_back(msgRef)should becomepush_back(msg), removing themsgRefcode entirely.Also, as a matter of style, it’s a bad idea to chain lots of
->operators together. It’s better to implement a method onUserin this case that adds aMessageto its own list and call that.