从lldb调用带有字符串参数的函数:取2
Invoking function with string argument from lldb: take 2
有时,我似乎不能使用lldb来调用带有字符串参数的函数。在这个问题中,用lldb调用带字符串参数的函数:如何?,我给出了一个简单的测试文件来说明这个问题。用户回答了这个问题,以显示在这种情况下修改测试文件将如何解决问题。
但是对于更一般的情况,我仍然无法从lldb调用带有字符串参数的函数。
要设置这个,我需要显示两个文件。第一个文件就像另一个问题一样,是一个简单的类lldbtest,它定义了一个接受字符串参数的构造函数。然后,另一个文件定义一个函数,其中调试器将中断。
第一个文件(不包含#include)如下所示:
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;
}
现在,当我在第二个文件的返回语句中设置断点时,并求值:
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,从一个文件以外的构造函数定义/声明,在这里?
编译用c++ -g -std=c++11。我使用这些相同的选项来链接目标文件,在Mac上也使用g++。
临时变量不能绑定到左值引用
问题是临时 string ("bar")
不能绑定到左值引用,这是您希望使用的构造函数作为参数;换句话说,在这种情况下,lldb
没有直接问题。
lldbtest::lldbtest(string &fum); // cannot be called with a temporary
要解决这个问题,您可以更改构造函数,或引入一个新的构造函数,它接受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)
如果你试图在main
中使用同一行代码,你将看到编译器不会接受你正在尝试做的事情:
lldbtest temporary (string ("bar")); // illegal, no suitable overload found
一个小例子
可以用下面的代码片段进一步解释:
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&) {
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数