为什么c++对象映射在Mavericks和Mountain Lion上不同?

Why do C++ Maps of Objects differ on Mavericks and Mountain Lion

本文关键字:Lion Mountain 对象 c++ 映射 Mavericks 为什么      更新时间:2023-10-16

我有一个问题,归结为以下内容:

我有一个c++结构,其中包含一个向量

struct Structure{                                                                                   
std::vector<int> c;
Structure();
Structure(std::vector<int> c_init);                                                                  
};
Structure::Structure( std::vector<int> c_init)                                                       
{c= c_init;
}                                                                                                    
Structure::Structure(){                                                                              
}                                                                                                    

和下面的程序

#include <iostream>
#include <vector>
#include <map>
#include "structr.h"

using namespace std;

int main () {
map<int, Structure*> StructureMap;
vector<int> foo;
foo.push_back(39);
foo.push_back(70);
foo.push_back(72);
StructureMap[1]=new Structure( foo);  
cout << (*StructureMap[1]).c[0] << "n" ; 
return 0;
}

用g++编译,输出是Mavericks和Mountain Lion上的"39",正如我假设的那样。但是,如果我在Mavericks上使用lldb逐步执行程序,我将收到

   20   StructureMap[1]=new Structure( foo); 
   21   
-> 22   cout << (*StructureMap[1]).c[0] << "n" ;
   23   
   24   return 0;
   25   }
(lldb) p *StructureMap[1]
(Structure) $6 = {
  c = size=3 {
    [0] = 39
    [1] = 70
    [2] = 72
  }
}

在Mountain Lion上我得到

   19   foo.push_back(72); 
   20   StructureMap[1]=new Structure( foo); 
   21   
-> 22   cout << (*StructureMap[1]).c[0] << "n" ;
   23   
   24   return 0;
   25   }
(lldb) p *StructureMap[1]
error: call to a function 'std::map<int,Structure*,std::less<int>,std::allocator<std::pair<const int, Structure*> > >::operator[](int const&)' that is not present in the target
error: Couldn't materialize struct: Structure hasn't been laid out yet
(lldb) 

为什么地图没有正确填写山狮,更让我困惑的是,为什么输出仍然是"39",即使地图是空的?

Thanks a million - Rob

映射被正确填充,如39所示的结果。从你的错误信息来看,Mountain Lion的lldb不理解Structure类型。这与地图本身无关。

这并不太令人惊讶。编写好的调试器是很困难的,正如Wikipedia所说"LLDB还处于早期开发阶段"