[英]String iterators incompatible ERROR when I convert string to vector<byte>
I have Const variable : 我有常量变量:
#define ERROR_ME std::string("Error Message")
in the function, I want return std::vector so, I write this: 在函数中,我想返回std :: vector,所以我这样写:
return std::vector<byte>(ERROR_ME.begin(), ERROR_ME.end()); // this have error
can you help me? 你能帮助我吗?
No you don't have a "Const [sic] variable", you have a symbolic constant that is replaced in the source before the compiler proper gets a chance to read it. 不,您没有“ Const [sic]变量”,您有一个符号常量,在编译器本身有机会读取它之前,该常量将在源代码中被替换。
The code the compiler will see is not 编译器将看到的代码不是
return std::vector<byte>(ERROR_ME.begin(), ERROR_ME.end());
but instead 但反而
return std::vector<byte>(std::string("Error Message").begin(), std::string("Error Message").end());
That is, you get the begin
and end
iterators from two different and unrelated objects. 也就是说,您从两个不同且不相关的对象获得了
begin
和end
迭代器。 Comparing or other interacting of unrelated iterators leads to undefined behavior . 不相关迭代器的比较或其他交互会导致未定义的行为 。
If you want a true constant then use eg 如果您想要一个真正的常数,请使用例如
std::string const ERROR_ME = "Error Message";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.