2016-07-07 19 views
-4

Ich verwende eine Multimap, um einige Daten mit Openframeworks zu speichern. Ich bin in der Lage, die Multimap zu erstellen, aber wenn ich versuche, die Daten darin zu drucken, kann ich nur die Speicheradresse drucken und nicht den Wert erhalten.Drucken von Wert aus Multimap

Referenz (Abschnitt "Speichern von Objekten in einer Karte"): http://openframeworks.cc/ofBook/chapters/stl_map.html

.h-Datei:

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-Datei:

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

i haben auch versucht:

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

Cut ist bis auf eine [MCVE] und entdecken Sie es absolut nichts mit 'multimap' oder Iteration zu tun. – juanchopanza

Antwort

0

Dieser Code wird nicht einmal kompiliert.

Wie dem auch sei, (*xyMapIterator).second (die als xyMapIterator->second geschrieben werden kann) hat xyPos geben, so können Sie es nicht drucken cout verwenden.

Wahrscheinlich müssen Sie seine Werte drucken:

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

Ich kann es falsch geschrieben haben, da es nicht genau das gleiche wie der Code in meinem Programm ist. das tut mir leid. Aber danke für die Antwort. Ich dachte nicht, es so zu drucken. – scribblePeople