Search code examples
c++pointersdictionaryvectorpush-back

Error using std::map::at


I have a map defined as:

map<std::string,std::vector<message *> > data;

Where std::string refers to a username (Primary key) and std::vector<message *> to the messages corresponding to a particular username. Message is a struct containing message id, time and the message text. Therefore, in order to push back another message, I use the following code:

std::vector<message *> messages = data.at[recvbuf_usrn];
messages.push_back(new message(*recvbuf_msg,currentDateTime()));
data[*recvbuf_usrn] = messages;

but I get an error:

error C3867: 'std::map<_Kty,_Ty>::at': function call missing argument list; use '&std::map<_Kty,_Ty>::at' to create a pointer to member

Where recvbuf_usrn and recvbuf_msg are defined as follows:

std::string *recvbuf_usrn=new std::string;
std::string *recvbuf_msg=new std::string;

How can I solve it?


Solution

  • It should be

    data.at(recvbuf_usrn);
    

    (atwith parents). or

    data[recvbuf_usrn];
    

    BTW, that return a reference, so you may simply do:

    auto& messages = data[recvbuf_usrn];
    messages.push_back(new message(*recvbuf_msg, currentDateTime()));
    

    You probably also should use smart pointer:

    std::map<std::string, std::vector<std::unique_ptr<message>>> data;