-4

i am using a multimap to store some data using openframeworks. i am able to create the multimap, but when i try to print the data within it, i am only able to print the memory address and not get the value.

reference (section "storing objects in a map"): http://openframeworks.cc/ofBook/chapters/stl_map.html

.h file:

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

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

i have also tried:

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 Answers1

0

This code does not even compile.

Anyway, (*xyMapIterator).second (which can be written as xyMapIterator->second) has type xyPos, so you cannot print it using cout.

Probably you need to print its values:

std::cout << xyMapIterator->second.x << "," << xyMapIterator->second.y << std::endl;
Mattia F.
  • 1,720
  • 11
  • 21
  • i may have written it incorrectly since it's not exactly the same as the code in my program. sorry for that. however, thanks for the response. i didn't think to print it that way. – scribblePeople Jul 09 '16 at 12:53