a very simple code like std::size_t f(const std::string& s) { return s.length(); }
, then in gdb p f("hello")
and maybe p f(std::string("hello"))
The function f
works in the code, however, (gdb) p f("hello") Attempt to take address of value not located in memory.
(gdb) set overload-resolution on (gdb) p f("hsdflo") Cannot resolve function f to any overloaded instance
. Using std:string
in gdb resulted in an syntax error.
解决方法1:
gdb
is not a compiler, it will not do the (not-so-)nice user-defined type conversions for you. If you wish to call a function that wants a string
, you need to give it a string
, not a const char*
.
Unfortunately, gdb
cannot construct an std::string
for you on the command line, again because it is not a compiler and object creation is not a simple function call.
So you will have to add a little helper function to your program, that would take a const char*
and return an std::string&
. Note the reference here. It cannot return by value, because then gdb
will not be able to pass the result by const reference (it's not a compiler!) You can choose to return a reference to a static object, or to an object allocated on the heap. In the latter case it will leak memory, but this is not a big deal since the function is meant to be called only from the debugger anyway.
std::string& SSS (const char* s)
{
return *(new std::string(s));
}
Then in gdb
gdb> p (paramNode_.px)->Get(SSS("domain"))
should work.
解决方法2:success after giving the command
set overload-resolution off