gdb 找不到运算符 []

2022-01-20 00:00:00 gdb vector c++
double var1, var2;
std::vector<double *> x;

var1 = 1;
var2 = 2;

x.push_back(&var1);
x.push_back(&var2);

当我在 gdb 中调试此代码并尝试 print x[0]*x[0] 时,我得到:

When I debug this code in gdb and try print x[0] or *x[0] I get:

找不到运算符[].

现在,如果我在 push_back 之后包含这一行:

Now if I include this line after the push_back:

x[0] = &var1;

我可以访问 gdb 中的任何特定元素.front()at() 等其他成员也会发生同样的情况.我的理解是编译器/链接器仅包含源中存在的成员函数代码,这些是我可以在 gdb 中使用的.有没有办法包含 std::vector 的每个成员函数,以便我可以在 gdb 中访问它们?

I can access any specific elements in gdb. The same thing happens with other members such as front(), at(), etc. My understanding is that the compiler/linker includes only the member functions present in the source code and those are the ones I can use in gdb. Is there a way to include every member function of std::vector so I can access them in gdb?

推荐答案

我的理解是编译器/链接器只包括源代码中存在的成员函数,这些是我可以在 gdb 中使用的.

My understanding is that the compiler/linker includes only the member functions present in the source code and those are the ones I can use in gdb.

您的理解不正确/不完整.

Your understanding is incorrect / incomplete.

std::vector 是一个模板类.如果没有显式实例化,编译器需要仅实例化调用的方法(通常是源中存在的方法的子集).

std::vector is a template class. Without explicit instantiation, the compiler is required to instantiate only the methods called (usually a subset of methods present in the source).

有没有办法包含 std::vector 的每个成员函数,以便我可以在 gdb 中访问它们?

Is there a way to include every member function of std::vector so I can access them in gdb?

对于给定类型 T,您应该能够通过请求显式实例化该 T 的 整个 向量,例如:

For a given type T, you should be able to explicitly instantiate entire vector for that T, by requesting it, e.g.:

template class std::vector<double>;

相关文章