views:

282

answers:

2

I hit a wall while debuging my C++ class. It's a graph of sorts, something like this:

class Graph {
    class Node {
        std::map<int, Node>::iterator _neighbors[4];
    };
    std::map<int, Node> _map;
};

Basically, each node keeps track of exactly 4 neighbors by storing iterators to them in the containing class' map.

The problem is that when I go to display the contents of _map in VS2008 during debugging, I get something like this:

- _map
  - [0]
      first
    - second
      - _neighbors
        - _ptr
            first
          - second
            - _neighbors
              - _ptr
                  first
                - second
                  - _neighbors
                  ...

Apparently, instead of listing the 4 neighbors of the Node with index 0 in the map, it lists its first neighbor, then the first neighbor's first neighbor, then its first neighbor and so on ad infinity. Also, at no point does _neighbors appear as an array, even though it is declared as such.

I found an add-on called VSEDebug that supposedly has enhanced STL display, but it's for VS2k3 and I couldn't get it to work in 2k8 (neither the binaries, nor compiling it myself).

The immediate window isn't much help either, as trying to call _map.operator[] returns with CXX0058: Error: overloaded operator not found.

Any ideas how I can get a meaningful display of the contents of my map? Note that I'm fairly new to VS as a whole, so I'll probably need detailed instructions. :)

+1  A: 

You can type the name of the symbol in the command window - immediate window and start following its member pointers. For example, if you debug a std::vector named v and want to access its element at position 2, type

*(v._Myfirst + 2)

Of course, the member _Myfirst depends on the implementation. But I think you get the idea. (visual studio has some problemas resolvindo overloads of the operators)

dudewat
`CXX0017: Error: symbol "_Myfirst" not found`. Tried looking around for something similar, but all VS displays for the map are the elements it contains. How can I figure out what `_Myfirst` is called in my implementation? Is it guaranteed to exist?
suszterpatt
There must be an internal member in your data-structure that actually points to the element. If your map is called *m*, start by typing *m* in the command window.
dudewat
+1  A: 

You can try your hand at writing a custom visualizer if you like, however you might end up duplicating functionality that already exists somewhat. Here's an article that goes over the basics:

http://www.virtualdub.org/blog/pivot/entry.php?id=120

If you just want to view all your array elements you can type "_map[0].second._neighbors,4" in the quick watch window to view it as an array of four, but this isn't exactly the fastest thing in the world.

Ron Warholic
`_map[0]` returns with `CXX0058: Error: overloaded operator not found`. dudewat's note regarding VS and operators seems appropriate
suszterpatt
Alternatively you could try "_map.find(0).second._neighbors,4" although this isn't guaranteed to work either (especially if no element exists with that key).
Ron Warholic