mardi 25 avril 2017

Store shared pointer to array in vector

I’m trying to store a shared pointer to a fixed size array in to a vector, I want to use a shared pointer because I must pass a pointer to the array to another class that will write in the array, and I want to have more than one array because I may have more instances of the writing class and each one needs an array to write to, they will write a lot of data in the arrays so moving them is not a good option.

std::shared_ptr<char> sp( new char [MAX_LENGTH], std::default_delete<  char[] >() );
arrayVect.push_back(sp);

the vector is defined as class member like:

std::vector< std::shared_ptr< char [ MAX_LENGTH ] > > arrayVect;

I'm getting the error:

 error: no matching function for call to ‘std::vector<std::shared_ptr<char [MAX_LENGTH]> >::push_back(std::shared_ptr<char []>&)’

I have tried different alternatives but none of them have worked, could you point out the correct way of doing it? or is there an alternative that I am missing? the writing class needs an array of chars for the write function so I think I’m stuck with array.

thanks!

Aucun commentaire:

Enregistrer un commentaire