1
votes

I have vector

 std::vector<OrderInfo *> vec

and a queue

queue<OrderInfo *> *myQueue = new queue<OrderInfo *>;

I want to copy the vector into the queue. I tried using How can I copy an entire vector into a queue? this answer and also this Insert into an STL queue using std::copy

but it's not working, how do I make it work?

this is what I tried: myQueue = new queue(vec.begin(), vec.end()); i got

error: no matching function for call to ‘std::queue::queue(std::vector::iterator, std::vector::iterator)’ myQueue = new queue(vec.begin(), vec.end());

and when I tried this:

std::copy(vec.begin(),vec.end(),std::back_inserter(myQueue));

i got:

required from ‘BacStrategy::BacStrategy(EZXConnectionHandler&, const string&, bool, const double&, int) [with Event_Type = EZXOrderEventHandler; std::__cxx11::string = std::__cxx11::basic_string]’ /home/yaodav/Desktop/git_repo/test/main.cpp:324:51: required from here /usr/local/include/c++/7.4.0/bits/stl_iterator.h:490:7: error: ‘std::queue*’ is not a class, struct, or union type operator=(const typename _Container::value_type& __value)

1
Please show your code and explain how it is not warking. The Q&As you linked are the way to go, we cannot know what you did wrong without seeing your code. Read about minimal reproducible example463035818_is_not_a_number
you might have problems with your pointers. Maybe what you really need is a std::vector<OrderInfo> and a queue<OrderInfo> instead of a vector of pointers and a pointer to a queue, this will make things easier a bit463035818_is_not_a_number
Why are there so many pointers? Why do you use new so much?Max Langhof
No. This is not the code that produces the error. In the code you pass *vec.begin() but the error complains about vec.begin(). Details do matter. Please do read this: minimal reproducible example463035818_is_not_a_number
@formerlyknownas_463035818 it was a copy past erroryaodav

1 Answers

5
votes

myQueue is a pointer, not a queue, and can’t be passed to std::back_inserter. To fix this, don’t declare it as a pointer.

Furthermore, std::back_inserter can’t be used with a std::queue, as the second link you posted explains.

Instead, simply write

std::queue<OrderInfo*> myQueue{
    std::deque<OrderInfo*>(vec.begin(), vec.end())
};

If you really need a pointer, adapt the code as follows:

std::queue<OrderInfo*>* myQueue = new std::queue<OrderInfo*>{
    std::deque<OrderInfo*>(vec.begin(), vec.end())
};

Lastly, if you need to fill an already initialised queue, proceed as follows: create a temporary queue using the above and assign it to your pointer:

*myQueue = std::queue<OrderInfo*>{std::deque<OrderInfo*>(vec.begin(), vec.end())};

If this looks too messy you can also create a temporary variable for that queue — but in that case you need to use std::move to ensure that the queue gets move-assigned, not expensively copied:

auto tmp = std::queue<OrderInfo*>{std::deque<OrderInfo*>(vec.begin(), vec.end())};
*myQueue = std::move(tmp);

In the same vein, consider carefully whether you want to store OrderInfos rather than pointers to OrderInfos.