将项目添加到 C++ 中的列表

发布于 2024-08-19 08:46:23 字数 585 浏览 9 评论 0原文

我在我的 C++ 应用程序中使用两个类。代码如下:

class MyMessageBox
{
public:
    void sendMessage(Message *msg, User *recvr);
    Message receiveMessage();
    list<Message> dataMessageList;
};

class User
{
public:
    MyMessageBox *dataMsgBox;
};

msg是一个指向Message类的派生类对象的指针。我已经实现了 sendMessage 函数,如下所示:

void MyMessageBox::sendMessage(Message *msg, User *recvr)
{
    Message &msgRef = *msg;
    recvr->dataMsgBox->dataMessageList.push_back(msgRef);
}

当我编译此代码时,出现以下错误: 对“消息的 vtable”的未定义引用。请帮我解决这个问题。

谢谢, 拉凯什。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖伴 2024-08-26 08:46:23

我不知道你想用 msgRef 做什么,但这是错误的。您曾经是一名 Java 程序员吗?

如果 MessageMessage 派生类的基类,则需要在列表中存储指针。将 list 更改为 list;并且 push_back(msgRef) 应变为 push_back(msg),完全删除 msgRef 代码。

另外,从风格上来说,将大量 -> 运算符链接在一起并不是一个好主意。在这种情况下,最好在 User 上实现一个方法,将 Message 添加到它自己的列表中并调用它。

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 Message is a base class for derivatives of Message, you need to store pointers in the list. Change list<Message> to list<Message*>; and push_back(msgRef) should become push_back(msg), removing the msgRef code 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 on User in this case that adds a Message to its own list and call that.

命硬 2024-08-26 08:46:23

对于初学者来说,如果要在标准 C++ 容器中存储多态对象,则应该存储指向该对象的指针,而不是基类的对象。如果不这样做,就会遇到对象切片问题。另外,帮自己一个忙,将指针包装在智能指针中以防止资源泄漏 - 我建议使用 boost::shared_ptr<>。

鉴于您没有向我们展示 Message 的代码,我们只能猜测问题是什么。由于它引用的是 vtable,因此可能是这样的:

  • 您没有将 Message 的任何类成员声明为virtual。从析构函数开始将是一个好主意
  • 您忘记链接到包含 Message 编译代码的对象文件

顺便说一句,在 sendMessage() 中创建附加引用是不必要的,恕我直言也不需要完全有助于可读性。只需在调用 push_back() 时取消引用 msg 指针即可。

For starters, if you want to store a polymorphic object in a standard C++ container, you should store a pointer to the object and not an object of the base class. If you don't, you run into object slicing issues. Also, do yourself a favour and wrap the pointer in a smart pointer to prevent resource leaks - I would recommend boost::shared_ptr<>.

Given that you haven't shown us the code for Message, we can but guess what the problem is. As it's referring to a vtable, chances are that:

  • You didn't declare any of Message's class members as virtual. Starting with the destructor would be a good idea
  • You forgot to link against an object file that contains the compiled code for Message

By the way, creating the additional reference in sendMessage() is not necessary and IMHO doesn't exactly help readability. Just dereference the msg pointer in your call to push_back().

仲春光 2024-08-26 08:46:23

如果要处理子类消息,则需要使用消息指针列表而不是消息对象,并管理它们的生命周期。为了方便起见,我建议

list< boost::shared_ptr<Message> > datamessageList

使用 boost 库。 (无意冒犯,但您确实需要多阅读一些有关 C++ 和指针的内容:看起来您尝试了代码的各种排列,直到获得编译结果...)

If you want to handle subclassed Messages, you need to use a list of Message pointers rather than Message objects, and manage their lifetime. For convenience, I'd suggest making it

list< boost::shared_ptr<Message> > datamessageList

using the boost library. (And not to offend, but you do need to read up a bit more on C++ and pointers: it looks like you tried various permutations of your code until you got something that compiled...)

花开柳相依 2024-08-26 08:46:23

我认为这是一个稍微人为的错误消息,表明您尚未为消息类实现构造函数。请查看此处这里...

鉴于您尝试将指针传递给对象列表时,编译器可能会抱怨它无法将 Message* 转换为 Message。尝试按照 Kylotan 的建议将列表更改为 Message* 列表。

是编译错误还是链接错误?

I think this is a slightly contrived error message suggesting that you've not implemented a constructor for your message class. Have a look here and here on SO...

Given that you're trying to pass a pointer to a list of objects, the compiler is probably complaining that it has no way to convert Message* to Message. Try changing your list to a list of Message* as Kylotan suggests.

Is it a compile or a link error?

梦里寻她 2024-08-26 08:46:23

由于有些人提出了更好的解决方案:请查看 std::queue 或 std::deque 对消息进行排队。所以现在你有:

std::queue<std::tr1::shared_ptr<Message> > dataMessageQueue;

As some are proposing better solutions: check out std::queue or std::deque to queue your messages. So now you have:

std::queue<std::tr1::shared_ptr<Message> > dataMessageQueue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文