我有一个名为nodes的unordered_map。声明如下:
unordered_map<T,shared_ptr<Vertex<T>>> nodes
Vertex是一个声明如下的类:
template<typename T>
struct Vertex {
Vertex(T pLabel) : label(pLabel) {};
void addEdge(T destination,int weight) {
edges[destination] = weight;
}
T label;
unordered_map<T, int> edges;
};
我有一个方法接受顶点的unoredered_map,如下所示。
template<typename T>
void getMinSpanningTree(unordered_map<T,shared_ptr<Vertex<T>>>& nodes, T first, int infinityMarker) {
cout << nodes.size() << endl;
if(nodes.size() == 0) {
return;
}
...
...
当我在LLDB中停止执行并输入以下内容时:
(lldb) expr nodes.size()
我得到以下输出:
(lldb) expr nodes.size()
error: Couldn't lookup symbols:
__ZNKSt3__113unordered_mapINS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEENS_10shared_ptrI6VertexIS6_EEENS_4hashIS6_EENS_8equal_toIS6_EENS4_INS_4pairIKS6_SA_EEEEE4sizeEv
所以,我不确定如何使调试器正确输出答案。以下行的标准输出,在输出控制台中打印5:
cout << nodes.size() << endl;
这里是我如何使用函数和类:
int main(int argc, const char * argv[]) {
shared_ptr<Vertex<string>> v1 = make_shared<Vertex<string>>("V1");
shared_ptr<Vertex<string>> v2 = make_shared<Vertex<string>>("V2");
shared_ptr<Vertex<string>> v3 = make_shared<Vertex<string>>("V3");
shared_ptr<Vertex<string>> v4 = make_shared<Vertex<string>>("V4");
shared_ptr<Vertex<string>> v5 = make_shared<Vertex<string>>("V5");
v1->addEdge("V2", 1);
v1->addEdge("V3", 3);
v2->addEdge("V1", 1);
v2->addEdge("V3", 3);
v2->addEdge("V4", 6);
v3->addEdge("V1", 3);
v3->addEdge("V2", 3);
v3->addEdge("V4", 4);
v3->addEdge("V5", 2);
v5->addEdge("V2", 3);
unordered_map<string,shared_ptr<Vertex<string>>> vertices;
vertices[v1->label] = v1;
vertices[v2->label] = v2;
vertices[v3->label] = v3;
vertices[v4->label] = v4;
vertices[v5->label] = v5;
getMinSpanningTree(vertices,v1->label,numeric_limits<int>::max());
return 0;
}
打印节点,似乎转储了所有内容:
(std::__1::unordered_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > > >) $0 = size=5 {
[0] = {
__cc = {
first = "V5"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208c68 strong=2 weak=1 {
__ptr_ = 0x0000000100208c68
}
}
__nc = {
first = "V5"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208c68 strong=2 weak=1 {
__ptr_ = 0x0000000100208c68
}
}
}
[1] = {
__cc = {
first = "V4"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208748 strong=2 weak=1 {
__ptr_ = 0x0000000100208748
}
}
__nc = {
first = "V4"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208748 strong=2 weak=1 {
__ptr_ = 0x0000000100208748
}
}
}
[2] = {
__cc = {
first = "V2"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208c08 strong=2 weak=1 {
__ptr_ = 0x0000000100208c08
}
}
__nc = {
first = "V2"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208c08 strong=2 weak=1 {
__ptr_ = 0x0000000100208c08
}
}
}
[3] = {
__cc = {
first = "V3"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x00000001002086e8 strong=2 weak=1 {
__ptr_ = 0x00000001002086e8
}
}
__nc = {
first = "V3"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x00000001002086e8 strong=2 weak=1 {
__ptr_ = 0x00000001002086e8
}
}
}
[4] = {
__cc = {
first = "V1"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208ba8 strong=2 weak=1 {
__ptr_ = 0x0000000100208ba8
}
}
__nc = {
first = "V1"
second = std::__1::shared_ptr<Vertex<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::element_type @ 0x0000000100208ba8 strong=2 weak=1 {
__ptr_ = 0x0000000100208ba8
}
}
}
}
答案 0 :(得分:0)
我很确定这与上面jtbandes给出的链接中描述的问题相同。如果我编译你的例子,那么做:
> nm a.out | grep size
>
没有匹配项,表明没有生成size
函数。注意,由于编译器通常不会发出模板函数实例化,如果代码不使用它们,你实际上应该在某处调用“size”以增加它被发出的机会。但即使有了这种改变,我仍然没有看到这种方法的产生。而是始终内联使用。
调试器几乎没有机会正确调用函数的内联版本。所以在这里帮助你的能力并不高。 clang&amp;上有一个bug。它关联的STL在-O0内联时不那么激进。这是解决这个问题的唯一实用解决方案。
链接问题中建议的解决方法是转发声明类。然而,我没有成功地提出通过编译器的这个类的前向声明。也许有些人有更强的C ++ - fu可以吗?
print
或frame variable
可以看到地图的元素并打印其大小,但这是通过一个单独的机制 - lldb“数据格式化程序”完成的。如果您想了解更多有关其工作原理的信息,请参考: