Question

I've created a list of queues and was wondering how I will be able to push back the queue to add a new element. I have multiples clients to send messages to and the host will send the exact same message to each in a specific order. I currently have it as

struct messages
{
    char buffer[64];
}

list<queue<messages>> MessageList;
for(int i = 0; i < numOfClients; i++)
{
    queue<messages> tempQueue;
    MessageList.push_back(tempQueue);
}
list<queue<messages>>::iterator MsgIt;
MsgIt = MessageList.begin();

messages storedMsg = {}
char tempMsg[64] = "This is the message to be stored!";
for(int j = 0; j < 64; j++)
    storedMsg.message[j] = tempMsg[j];
for(int i = 0; i < numOfClients; i++)
{
    *MsgIt.push(storedMsg);
    MsgIt++;
}

There appears to be an error in which *MsgIt will not allow me to access the push() function.

I understand that

queue<messages> MessageList;
MessageList.push(Message);

will work, but since there is multiple clients receive this message, I wish to use it in a "list of queues" instead of creating 15-20 or so queues which can be hard to manage.

Was it helpful?

Solution 2

Just use MsgIt->push(Message) instead of the awkward dereference. The . operator has a higher precedence than the * operator, so what's happening is that it's trying to call . on the iterator rather than what the iterator points to, then trying to dereference the returned value, neither of which are what you want. You'd have to do (*MsgIt).push to get the desired result using dereferencing.

Also your use of iterators isn't really how you should do it. It should look like:

for(MessageList::iterator MsgIt = MessageList.begin(), MsgIt < MessageList.end(), MsgIt++)

Finally, as @ChronoTrigger mentioned, you haven't actually placed any queues in the MessageList list, at least in this code. So there is nothing to push to in the first place.

OTHER TIPS

*MsgIt is of type queue<messages>, and you are trying to insert a variable of type char[64].

You should insert first a queue and then , in the queue, your message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top