使用lldb中的字符串参数调用函数:取2

kdo*_*dog 5 c++ string lldb c++11

我似乎无法使用lldb来调用带有字符串参数的函数.在这个问题中,使用字符串参数调用函数与lldb:如何?,我给出了一个简单的测试文件来说明问题.用户回答了该问题,以显示在这种情况下修改测试文件如何解决问题.

但是对于更一般的情况,我仍然无法使用lldb中的字符串参数调用函数.

要设置它,我需要显示两个文件.第一个文件就像在另一个问题中一样,一个简单的类lldbtest定义了一个带字符串参数的构造函数.然后,另一个文件定义调试器将中断的函数.

第一个文件(没有一些#includes)看起来像这样:

 struct lldbtest{
   int bar=5;
   lldbtest();
   lldbtest(int foo);
   lldbtest(string &fum);
 };

 lldbtest::lldbtest() { bar=6; }
 lldbtest::lldbtest(int) { bar=7; }
 lldbtest::lldbtest(string&) { bar=8; }

 int main(){
   string name="fum";
   lldbtest x,y(3);
   cout<<x.bar<<y.bar<<endl;
   int zz=nothing();

   return 0;
 }
Run Code Online (Sandbox Code Playgroud)

第二个文件如下所示:

 using namespace std;
 int nothing(){
   cout<<"hello there"<<endl;
   int n=0,m=1,r=2;
   return m+r+n;
 }
Run Code Online (Sandbox Code Playgroud)

现在,当我在第二个文件的return语句中设置断点时,并评估:

p lldbtest(string("bar"))
Run Code Online (Sandbox Code Playgroud)

我收到一条很长的错误消息,很难在这里发布,但看起来像这样:

error: no matching conversion for functional-style cast from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest'
 note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'const lldbtest' for 1st argument
note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest' for 1st argument
note: candidate constructor not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'int' for 1st argument
note: candidate constructor not viable: expects an l-value for 1st argument
note: candidate constructor not viable: requires 0 arguments, but 1 was provided
error: 1 errors parsing expression
Run Code Online (Sandbox Code Playgroud)

哇,错误信息比代码长.

无论如何,我的问题是:如何使用lldb中的字符串参数调用函数/构造函数,而不是定义/声明构造函数的文件,如此处所示?

汇编是用g ++ -g -std = c ++ 11编写的.我使用这些相同的选项链接目标文件,也使用Mac上的g ++.

Fil*_*efp 7

临时工具不能绑定左值参考

问题是临时的,string ("bar")不能绑定到左值引用,这是你想要使用的构造函数作为参数; 换句话说,lldb在这种情况下没有直接的问题.

lldbtest::lldbtest(string &fum); // cannot be called with a temporary
Run Code Online (Sandbox Code Playgroud)

要解决这个问题,您可以更改构造函数,或者引入一个新的构造函数,它接受一个string const&或只是一个string,它适用于在以下期间传递的临时值p lldbtest(string("bar")):

lldbtest::lldbtest(string const&fum); // can bind to a temporary
lldbtest::lldbtest(string fum2);      // will copy the argument, safe to use with
                                      // lvalue and rvalues (temporaries)
Run Code Online (Sandbox Code Playgroud)

如果您尝试使用相同的代码行,main您将看到编译器不会接受您尝试执行的操作:

lldbtest temporary (string ("bar")); // illegal, no suitable overload found
Run Code Online (Sandbox Code Playgroud)

一个小例子

这可以通过以下小片段进一步解释:

void func (std::string&) {
  // ...
}

int main () {
  func (std::string ("bar"));
}
Run Code Online (Sandbox Code Playgroud)

foo.cpp: In function ‘int main()’:
foo.cpp:8:28: error: invalid initialization of non-const reference of type ‘std::string&
                     {aka std::basic_string<char>&}’ from an rvalue of type ‘std::string
                     {aka std::basic_string<char>}’
   func (std::string ("bar"));
                            ^
foo.cpp:3:6: note: in passing argument 1 of ‘void func(std::string&)’
 void func (std::string&) {
Run Code Online (Sandbox Code Playgroud)