I'm creating a unique_ptr to a struct (which works fine):
std::unique_ptr<w9::Product> product (new w9::Product(desc[i].desc, price[j].price));
After which, I am then appending that unique_ptr onto a vector member function.
object += product;
Which overloads the += as follows:
void operator+=(std::unique_ptr<Product> const &p) {
object.push_back(&p);
}
The problem is this last bit. What is the proper way to push_back a unique ptr onto a vector?
EDIT:
Object is template class object that contains a vector as a data member. std::vector list
template <typename T>
class List {
std::vector<T> list;
}
T in this case would be a 'unqiue_ptr'.
Actual code:
w9::List<w9::Product> merge(const w9::List<w9::Description> &desc, const w9::List<w9::Price>& price) {
w9::List<w9::Product> priceList;
for(int i = 0; i < desc.size(); i++){
for(int j = 0; j < price.size(); j++){
if(price[j].code == desc[i].code){
std::unique_ptr<w9::Product> product (new w9::Product(desc[i].desc, price[j].price));
product->validate();
priceList += std::move(product);
}
}
}
return priceList;
}
void operator+=(std::unique_ptr<w9::Product> &&p) {
list.push_back(std::move(p));
}
Error:
./List.h:41:12: error: no matching member function for call to 'push_back'
list.push_back(std::move(p));
~~~~~^~~~~~~~~
w10.cpp:18:15: note: in instantiation of member function 'w9::List<w9::Product>::operator+=' requested here
priceList += std::move(product);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:700:36: note: candidate function not viable: no
known conversion from 'typename remove_reference<unique_ptr<Product, default_delete<Product> > &>::type' (aka 'std::__1::unique_ptr<w9::Product,
std::__1::default_delete<w9::Product> >') to 'const value_type' (aka 'const w9::Product') for 1st argument
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);