Reputation: 1
char * val = ep->GetValue();
std::string m="zValue:" + (std::string)val;
ep->GetValue() returns a char pointer. I want to append it to a string. I tried as above but it throws the following error.
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Upvotes: 0
Views: 1297
Reputation: 12956
Or in reusable form :
#include <string>
#include <iostream>
std::string string_from_ptr(const char* p)
{
std::string retval;
if (p != nullptr) retval = p;
return retval;
}
int main()
{
char* p{ nullptr };
std::cout << string_from_ptr(p);
// auto str = string_from_ptr(ep->GetValue());
}
Upvotes: 0
Reputation: 1988
You get this error because you try to build a std::string from a nullptr
. Try:
std::string m = "zValue:";
if(char * val = ep->GetValue()) m += val;
Upvotes: 4