0

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.

Community
  • 1
  • 1
ama
  • 1
  • 1

1 Answers1

1

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

LihO
  • 41,190
  • 11
  • 99
  • 167
  • Thank you for your answer. First, I made a silly mistake referring to it as an "array of vectors" - I meant of course just a vector. Thanks for spotting this, I edited my question to reflect the change. The code though stays the same as in the original formulation of my question. My problem is at debugging (in vs10): when adding either vec[7] or vec[7].a (as in your example) to the watch list I get the error "CXX0058: Error: overloaded operator not found". Does it just work well for you without having made any changes to the debugger settings? – ama Feb 05 '12 at 15:05
  • If you have just a simple vector and you are running your program under Debug configuration, you should just open "Autos" window and there should be vector `vec` displayed properly. – LihO Feb 05 '12 at 15:46
  • This is strange - I see `vec` but it is definitely not displayed properly, e.g. if vec.size()==3 then debugger displays vec as follows: `[3]({a=??? b=??? },{a=??? b=???? },{a=??? b=??? })`. I wonder then if this is happening only for me. From reading other posts I figured this is standard behavior of VS debugger for templated types, unless you tweak the debugger to recognize your types. Maybe I misunderstood. I'll try running the same code on another machine later. Either way thanks for your help! – ama Feb 05 '12 at 16:45
  • You're welcome. I would appreciate if you accept my answer or edit your question and provide more information so that I truly know what you are dealing with. – LihO Feb 05 '12 at 16:51
  • What further information do you think I should add? The class I gave as example is not a simplified version of my problem; the debugger really does give the error codes I've written in the question (and in my last comment here) even for this simple class. Maybe I should add I am compiling using VS2010 on a Win7 x64 machine. – ama Feb 05 '12 at 17:08
  • If it's as you say, then it's really weird. For me it works perfectly. – LihO Feb 05 '12 at 17:15