J'utilise boost 1.37, et j'essaie d'utiliser boost :: ptr_vector, et j'en transfère la propriété pour que je puisse le renvoyer d'une fonction. En regardant la documentation de boost (http://www.boost.org/doc/libs/1_36_0/libs/ptr_container/doc/tutorial.html#new-functions)Lance un boost :: ptr_vector, ne correspond pas à la documentation
std::auto_ptr< boost::ptr_deque<animal> > get_zoo()
{
boost::ptr_deque<animal> result;
...
return result.release(); // give up ownership
}
...
boost::ptr_deque<animal> animals = get_zoo();
J'ai essayé:
#include "iostream"
#include <boost/ptr_container/ptr_vector.hpp>
class Item
{
public:
int my_val;
Item() : my_val(0) { }
};
class MyClass
{
private:
boost::ptr_vector<Item> items_;
public:
MyClass()
{
for (int i = 0; i < 10; ++i)
items_.push_back(new Item);
}
std::auto_ptr<boost::ptr_vector<Item> > getData() { return items_.release(); }
};
int totalItems(boost::ptr_vector<Item> items)
{
int total = 0;
boost::ptr_vector<Item>::iterator it;
for (it = items.begin(); it != items.end(); ++it)
total += (*it).my_val;
return total;
}
int main(int argc, char **argv)
{
MyClass cls;
boost::ptr_vector<Item> items = cls.getData();
int total = totalItems(items);
fprintf(stdout, "I found %d items!\n", total);
return 0;
}
erreurs de compilation:
In function ‘int main(int, char**)’:
error: conversion from ‘std::auto_ptr<boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> > >’ to non-scalar type ‘boost::ptr_vector<Item, boost::heap_clone_allocator, std::allocator<void*> >’ requested
Est-ce un petit problème dans la documentation de boost? Puis-je obtenir un auto_ptr et un dereference pour récupérer le ptr_vector?
Merci pour le heads up, tout fonctionne maintenant. – kevbo