Abanoub
Abanoub

Reputation: 3809

no suitable constructor exists to convert from "uint8_t *" to "std::vector<uint8_t, std::allocator<uint8_t>>"

no suitable constructor exists to convert from "uint8_t *" to "std::vector<uint8_t, std::allocator<uint8_t>>"

and the cast not working

Edit:

const std::vector<uint8_t> Test (const std::vector<uint8_t> buffer) const;

uint8_t* buffer="...";

//so i can use Test() function
Test(buffer);

Error
no suitable constructor exists to convert from "uint8_t *" to "std::vector<uint8_t, std::allocator<uint8_t>>"

Upvotes: 2

Views: 3285

Answers (1)

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

You cannot convert an array to a std::vector, you need to construct one explicitly. One way to do it would be to use the vector's range constructor like this:

uint8_t* buffer="...";
// +1 for the terminating \0
std::vector<uint8_t> vector( buffer, buffer + strlen( buffer ) + 1 ); 
Test( vector );

As a side note if your buffer has embedded \0 then strlen will return an incorrect value. As a workaround you can do something like this:

uint8_t[] buffer="...";
std::vector<uint8_t> vector( buffer, buffer + sizeof( buffer ) ); 
Test( vector );

Upvotes: 4

Related Questions