gpt4 book ai didi

c++ - 在 C++ lldb 中使用重载运算符评估表达式

转载 作者:IT老高 更新时间:2023-10-28 21:59:55 26 4
gpt4 key购买 nike

我正在使用 lldb 在 Xcode 5 中调试 C++ 程序,我想在调试器中评估任意表达式,尤其是那些使用重载运算符的表达式。

例如,我创建了一个非常简单的 Xcode 5 C++ 项目,其中包含以下 main.cpp 和所有编译器/链接器/等选项设置为默认值:

#include <iostream>
#include <vector>

int main(int argc, const char * argv[])
{
std::vector<int> vec;
vec.push_back(42);
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}

我在 return 0; 行设置断点并运行程序。

然后,在 lldb 提示符下,将 vector 作为一个整体打印就可以了:

(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) $0 = size=1 {
[0] = 42
}

但是,我无法使用重载的 operator[] 访问其成员:

(lldb) expr vec[0]
error: call to a function 'std::__1::vector<int, std::__1::allocator<int> >::operator[](unsigned long)' ('_ZNSt3__16vectorIiNS_9allocatorIiEEEixEm') that is not present in the target
error: The expression could not be prepared to run in the target

同样,我无法获取迭代器(虽然我在这里经验较少,所以我的语法可能是错误的):

(lldb) expr vector<int>::iterator it = vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression

(lldb) expr (vector<int>::iterator) vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression

类似地,打印一个简单的字符串也可以正常工作:

(lldb) expr string("a")
(std::__1::string) $0 = "a"

但是,简单的字符串连接失败:

(lldb) expr string("a") + string("b")
error: invalid operands to binary expression ('string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') and 'string')
error: 1 errors parsing expression

我做错了什么? lldb 是否支持重载运算符的求值?

提前谢谢你!

最佳答案

我刚刚遇到了同样的问题,显然找到了一个简单的解决方法。您可以像这样访问 vector vec 的第 i 个元素:

(lldb) p vec.__begin_[i]
(int) $1 = 100

关于c++ - 在 C++ lldb 中使用重载运算符评估表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com