我已经使用 http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F 配置了 pretty-print .它成功地适用于 vector 和其他容器。但是我无法像下面的示例那样检查 map :
#include <map>
#include <iostream>
using namespace std;
int main ()
{
map <int, string> mapIntToString;
map <int, int> mapInt2;
mapIntToString.insert (map <int, string>::value_type (3, "Three"));
mapInt2.insert (map <int, int>::value_type (3, 4));
return 0;
}
使用 gdb 打印时出现以下错误:
(gdb) p mapInt2
$1 = std::map with 1 elementsTraceback (most recent call last):
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 422, in children
rep_type = find_type(self.val.type, '_Rep_type')
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 45, in find_type
raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
ValueError: Cannot find type std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::_Rep_type
最佳答案
您使用什么编译器(以及哪个版本)来构建您的测试源?
我猜它不是 g++
的最新版本。这是我使用 g++ 4.4.3-4ubuntu5
得到的结果:
$ gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) b 12
Breakpoint 1 at 0x400de3: file t.cc, line 12.
(gdb) r
Breakpoint 1, main () at t.cc:12
12 return 0;
(gdb) p mapInt2
$1 = std::map with 1 elements = {[3] = 4}
更新:
This is what I get for the version: g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
我看到了问题。您引用的说明不正确。
特别是说明提示:svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
,但问题是python代码到达进入 libstdc++
internals,因此 必须 匹配这些内部结构(这就是 pretty-print 是 GCC 的一部分而不是 GDB 的一部分的原因,a事实上 bruce.banner 提示过)。
当您执行新的 svn co ...
时,您选择了一份 不再 与您的 libstdc++
内部匹配的 python 代码拷贝,这就是导致您出现问题的原因。
特别是,svn log
显示这里添加了find_type
:
r183732 | tromey | 2012-01-30 08:25:11 -0800 (Mon, 30 Jan 2012) | 27 lines
这比 gcc-4.4.3
晚了很多。然后,您要做的是获得与 您的 libstdc++
版本相匹配的 pretty-print ,如下所示:
svn co svn://gcc.gnu.org/svn/gcc/branches/gcc_4_4_3_release/libstdc++-v3/python
除了上面的命令将不起作用,因为 gcc 4.4.3 早于 pretty-print 。
无论如何,std::map
的实现(以及其他大部分 STL 内部结构)在 4.4.3 和 4.6 之间没有改变,而这个命令确实 工作:
svn co svn://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python
关于c++ - 漂亮的 map 打印机抛出类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102967/