【问题标题】:Invoking function with string argument from lldb: take 2使用来自 lldb 的字符串参数调用函数:取 2
【发布时间】:2014-07-29 03:22:16
【问题描述】:

有时,我似乎无法使用 lldb 调用带有字符串参数的函数。在这个问题 Invoking function with string argument with lldb: how? 中,我给出了一个简单的测试文件来说明问题。一位用户回答了这个问题,以说明在这种情况下修改测试文件将如何解决问题。

但对于更一般的情况,我仍然无法从 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;
 }

第二个文件如下所示:

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

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

p lldbtest(string("bar"))

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

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

哇,错误信息比代码长。

无论如何,我的问题是:如何从 lldb 中的字符串参数调用函数/构造函数,而不是像这里定义/声明构造函数的文件?

使用 g++ -g -std=c++11 编译。我使用这些相同的选项来链接目标文件,在 Mac 上也使用 g++。

【问题讨论】:

    标签: c++ string c++11 lldb


    【解决方案1】:

    临时对象不能绑定到左值引用

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

    lldbtest::lldbtest(string &fum); // cannot be called with a temporary
    

    要解决此问题,您可以更改构造函数,或引入一个新的构造函数,它接受string const&amp; 或仅接受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)
    

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

    lldbtest temporary (string ("bar")); // illegal, no suitable overload found
    

    一个小例子

    这可以用下面的小sn-p进一步解释:

    void func (std::string&) {
      // ...
    }
    
    int main () {
      func (std::string ("bar"));
    }
    

    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&) {
    

    【讨论】:

    • 非常感谢!你不会相信我花了多长时间试图弄清楚这一点,而这只是一个角色。无论如何,这完全解决了问题。
    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多