简体   繁体   English

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

[英]Adding item to list in C++

I am using two classes in my C++ application. 我在C ++应用程序中使用了两个类。 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. msg是指向Message类的派生类对象的指针。 I have implemented the function sendMessage as follows: 我已经实现了sendMessage函数,如下所示:

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'. 编译此代码时,出现以下错误:未定义对`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. 我不知道您要使用该msgRef做什么,但这是错误的。 Are you an ex-Java programmer, by any chance? 您是前Java程序员吗?

If Message is a base class for derivatives of Message , you need to store pointers in the list. 如果MessageMessage派生的基类,则需要在列表中存储指针。 Change list<Message> to list<Message*> ; list<Message>更改为list<Message*> and push_back(msgRef) should become push_back(msg) , removing the msgRef code entirely. push_back(msgRef)应该变成push_back(msg) ,完全删除msgRef代码。

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. 在这种情况下,最好在User上实现一个将Message添加到其自己的列表中并对其进行调用的方法。

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. 对于初学者,如果要将多态对象存储在标准C ++容器中,则应存储指向该对象而不是基类对象的指针。 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<>. 另外,请帮自己一个忙,并将指针包装在智能指针中以防止资源泄漏-我建议使用boost :: shared_ptr <>。

Given that you haven't shown us the code for Message, we can but guess what the problem is. 鉴于您尚未向我们显示Message的代码,我们只能猜测问题出在哪里。 As it's referring to a vtable, chances are that: 提到vtable时,可能是:

  • You didn't declare any of Message's class members as virtual . 您没有将Message的任何类成员声明为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 您忘记链接到包含Message编译代码的目标文件

By the way, creating the additional reference in sendMessage() is not necessary and IMHO doesn't exactly help readability. 顺便说一句,没有必要在sendMessage()创建其他引用,并且IMHO不能完全帮助提高可读性。 Just dereference the msg pointer in your call to push_back(). 只需在对push_back()的调用中取消引用msg指针。

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 . 鉴于您尝试传递指向对象列表的指针,编译器可能会抱怨它无法将Message*转换为Message Try changing your list to a list of Message* as Kylotan suggests. 按照Kylotan的建议,尝试将您的列表更改为Message*列表。

Is it a compile or a link error? 是编译错误还是链接错误?

If you want to handle subclassed Messages, you need to use a list of Message pointers rather than Message objects, and manage their lifetime. 如果要处理子类化的Message,则需要使用Message指针列表而不是Message对象,并管理其生存期。 For convenience, I'd suggest making it 为了方便起见,我建议这样做

list< boost::shared_ptr<Message> > datamessageList

using the boost library. 使用boost库。 (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...) (并且不要冒犯,但是您确实需要更多地阅读C ++和指针:看起来您尝试了对代码的各种排列,直到获得编译的内容为止……)

As some are proposing better solutions: check out std::queue or std::deque to queue your messages. 正如某些人提出的更好的解决方案一样:请检查std :: queue或std :: deque对消息进行排队。 So now you have: 现在,您有了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM