1

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)

5
  • 2
    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 example Jan 23, 2020 at 12:49
  • 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 bit Jan 23, 2020 at 12:52
  • 3
    Why are there so many pointers? Why do you use new so much? Jan 23, 2020 at 12:54
  • 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 example Jan 23, 2020 at 12:56
  • @formerlyknownas_463035818 it was a copy past error
    – yaodav
    Jan 23, 2020 at 12:58

1 Answer 1

5

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.

9
  • and now Im :getting /usr/local/include/c++/7.4.0/bits/stl_iterator.h:492:13: error: ‘class std::queue<OrderInfo*>’ has no member named ‘push_back’ container->push_back(__value); ~~~~~~~~~~~^~~~~~~~~
    – yaodav
    Jan 23, 2020 at 13:02
  • @yaodav Yes, as is explained in the link you’ve posted. My bad. The updated code works now. Jan 23, 2020 at 13:08
  • thank you very much but I need it to be a ptr because the queue is a data member and the vector comes later on. so il just do it in the old fashion way (loop)
    – yaodav
    Jan 23, 2020 at 13:47
  • @yaodav I’m not sure I understand this: data members don’t need to be pointers! That said, I’ll update my answer. No loops needed. Jan 23, 2020 at 13:49
  • what I mean is - from what I know is that if I have a none ptr data member then its init in the constructor, then I cant use your method. but if I use ptr (and I'm not considering the other aspect of ptr vs value) I can create the queue when the vector coming
    – yaodav
    Jan 23, 2020 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.