ama
ama

Reputation: 1

What do I need to enter in autoexp.dat in order to be able to inspect a data member of a class contained in a vector?

First, an apology: I've read some previous threads about inspecting templates in VS (e.g. this one); unfortunately they either did not contain the information I need or (more likely) I was not able to extract the information successfully from the answers. I hope I'm not asking something too obvious that has been asked many times before.

I have a vector containing objects of class SomeClass:

class SomeClass {
    int a, b;
    //constructor..
};

std::vector<SomeClass> vec;

//vec.push some elements..

Now I want to be able to watch vec[0].a, vec[1].a, .... in the VS2010 debugger. When I naively try this I ofcourse get:

vec[0].a    CXX0058: Error: overloaded operator not found

And when I try one of the solutions offered in the answers to the question I linked to above, I get:

((vec)._Myfirst)[0].a   CXX0025: Error: operator needs class/struct/union

So I understand I need to modify autoexp.dat. I tried doing this for a while with no success whatsoever.

I would really appreciate it if someone could write what line/s I need to add to autoexp.dat in order to be able to inspect these variables (I already feel that I've spent way too much time on this - and so I would be very grateful if I could get an explicit solution rather than hints or links).

Thank you for your time.

Upvotes: 0

Views: 493

Answers (1)

LihO
LihO

Reputation: 42103

Let SomeClass be defined like this:

class SomeClass
{
public:
    SomeClass(int a, int b) : a(a), b(b) { }
    int a, b;
};

You say that you have "an array of vectors", but you have just the vector of objects of type SomeClass:

std::vector<SomeClass> vec;
SomeClass a(1,2);
vec.push_back(a);
std::cout << vec[0].a;

In this case, you can access them directly using array subscript operator ([ ]). If you are sure there is object at index 0, then vec[0].a is just fine.

If you need an array of vectors, it would look like this:

std::vector<SomeClass> vec[10];
SomeClass a(1,2);
vec[3].push_back(a);
std::cout << vec[3][0].a;

You declare vec as an array of 10 vectors and then you pushes an element a at the end of 4. vector (at index 3). By vec[3][0].a you are accessing attribute a of the element at index 0 of the vector at index 3.

Output of both of these examples is: 1

And for debugging:
In second example when I toggle breakpoint at line vec[3].push_back(a);, then I select Debug configuration, then I press F5 and when it stops at my breakpoint:

  1. Press Ctrl + D, Q
  2. Write vec[7] into Expression field
  3. Select Add Watch
  4. See how lovely Visual Studio shows you vector at index 7 in the Watch 1 window

Hope this helps

Upvotes: 1

Related Questions