euskadi
euskadi

Reputation: 127

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?

Upvotes: 0

Views: 2267

Answers (2)

songyuanyao
songyuanyao

Reputation: 172904

std::map::at is function, you should call it as:

std::vector<message *> messages = data.at(*recvbuf_usrn);

If you want to use std::map::operator[] you should:

std::vector<message *> messages = data[*recvbuf_usrn];

Upvotes: 1

Jarod42
Jarod42

Reputation: 217235

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;

Upvotes: 1

Related Questions