没有合适的构造函数可以从“uint8_t *”转换为“std :: vector <uint8_t,std :: allocator <uint8_t =”“>&gt;”</uint8_t,>

时间:2011-02-27 18:49:35

标签: c++

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

并且演员无效

编辑:

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>>"

1 个答案:

答案 0 :(得分:4)

您无法将array转换为std::vector,您需要明确构建一个。 一种方法是使用vector的范围构造函数,如下所示:

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

如果您的缓冲区已嵌入\0,那么strlen将返回不正确的值。作为一种解决方法,您可以执行以下操作:

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