2016-07-07 66 views
-4

我正在使用multimap来存储一些使用openframeworks的数据。我能够创建multimap,但是当我尝试打印其中的数据时,我只能够打印内存地址并且不能获取值。无法打印multimap中的值

参考(部分 “在地图存储对象”):http://openframeworks.cc/ofBook/chapters/stl_map.html

h文件:

class xyPos { 
     public: 

     float x, y; 

     xyPos(float xPos, float yPos) { 
      x = xPos; 
      y = yPos; 
    } 

    //return ofVec2f(x, y); 
}; 

static multimap<string, xyPos> posMap; 
static multimap<string, xyPos>::iterator xyMapIterator; 

cpp文件:

for(int i = 0; i < 10; i ++) { 
    for(int j = 0; j < 10; j ++) { 
     posMap.insert(make_pair("null", xyPos(i, j)); 
    } 
} 

我也曾尝试:

for(int i = 0; i < 10; i ++) { 
    for(int j = 0; j < 10; j ++) { 
     xyPos *p = new xyPos(i, j); 
     xyMap.insert(make_pair("null", *p); 
    } 
} 

cout << "xyMap:\n"; 
for(xyMapIterator = xyMap.begin(); xyMapIterator != xyMap.end(); ++ xyMapIterator) { 
    cout << (*xyMapIterator).first << " => " << (*xyMapIterator).second << "\n"; 
} //will only compile with &(*xyMapIterator).second so i only have ["null", memory address] in the output 
+1

切是下降到[MCVE],发现它绝对没有任何关系'multimap'或迭代。 – juanchopanza

回答

0

这段代码甚至没有编译。

无论如何,(*xyMapIterator).second(可写为xyMapIterator->second)的类型为xyPos,因此无法使用cout进行打印。

也许你需要打印的价值观:

std::cout << xyMapIterator->second.x << "," << xyMapIterator->second.y << std::endl; 
+0

我可能写错了,因为它与我的程序中的代码不完全相同。对不起。不过,谢谢你的回应。我不认为要这样打印。 – scribblePeople