This is a follow on from an earlier question about how to condense a switch statement. I have used the input provided earlier but I am getting errors about how to insert my elements into the vector, but I thought this was how you inserted a vector in C++?
std::queue<myStruct > myQueue1, myQueue2, myQueue3, myQueue4, myQueue5
void change(float posx, float posy, int idNumber){
myStruct newDir;
newDir.psX = posx;
newDir.psY = posy;
std::vector< std::queue<myStruct> > myVector (5);
myVector.begin();
myVector.insert (myQueue1);
myVector.insert (myQueue2);
myVector.insert (myQueue3);
myVector.insert (myQueue4);
myVector.insert (myQueue5);
if (idNumber >= 1 && idNumber<= 5){
myVector[idNumber-1].push (newDir);
}
}
TIA Any help appreciated.
vector::insert()is for inserting a new element at a particular position (so it needs a position parameter). You probably just wantpush_back()which will put the new item at the end of the vector. For this to work, you wouldn’t want to initialize your vector as you do (which puts five empty queues on the vector); just default initialize the vector:But there’s also the issue that your vector has elements with type
std::queue<locaRef>, but the queues you’re putting on the vector arestd::queue<MyStruct>. even if you fix the difference in types, you’ll need to keep in mind that what will get put on to the vector are copies of the queue, which may or may not be what you want. You may want the vector to take some kind of pointer or smart pointer to your queue objects if you don’t want copies.