【发布时间】:2021-09-08 10:02:44
【问题描述】:
char * val = ep->GetValue();
std::string m="zValue:" + (std::string)val;
ep->GetValue() 返回一个字符指针。我想将它附加到一个字符串。我按照上面的方法尝试了,但它会引发以下错误。
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
【问题讨论】:
-
我明白
val == nullptr...所以错误 -
虽然与您的问题无关,但几乎任何时候您觉得需要进行 C 风格转换或转换(就像您对
(std::string)val所做的那样),您应该将其视为您正在做的标志有事吗。如果要创建字符串,请使用std::string(val)显式执行。 -
ep_>GetValue()返回空值。当ep->GetValue()返回 null 时,您希望字符串是什么样的?