The syntax of moving std::unique_ptr<...>() somewhat eludes me, and i can't find a clear answer(to me at least), as to how i should move the unique_ptr around.
I have some heap-allocated Nodes and would like to create new Nodes, which have two already existing Nodes as children. Which in turn are supposed to be inserted into the vector.
#include <memory> #include <vector> template <typename T> struct Node{ std::unique_ptr<Node<T>> left, right; Node(std::unique_ptr<Node<T>> left, std::unique_ptr<Node<T>> right){ this->left = left; this->right = right; } } template <typename T> void foo(){ std::vector<std::unique_ptr<Node<T>>> vec; //... vec.insert(vec.begin() + idx, std::unique_ptr<Node<T>>(new Node<T>(vec.at(vec.begin() + idx), vec.at(vec.begin() + idx + 1)))); } I only get the error-meassage, that no matching function call was found.
expression.hpp:69:58: error: no matching function for call to ‘std::vector<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >, std::allocator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > > > >::at(__gnu_cxx::__normal_iterator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >*, std::vector<std::unique_ptr<Node<int>, std::default_delete<Node<int> > >, std::allocator<std::unique_ptr<Node<int>, std::default_delete<Node<int> > > > > >)’ Cann somebody help, or has an idea, where i can look for the correct syntax, and which move/copy-function i should use?
std::vector'satmember function takes an integer (technically asize_t) and not an iterator, so I think you can just use idx and idx + 1 directly in the calls toat. It's not about syntax, but about the types. I suspect that your error message says as much.