Why can't I index a std::vector in the immediate window?
Asked Answered
D

2

15

So, I have a vector

 std::vector<std::string> lines. 

I fill this vector up, and can access it like

 std::string temp = lines[0];

However, in the immediate window, both

 lines[0] - error:overloaded operator not found

and

 lines.at(0) - error:symbol is ambiguous

don't work at all. Is there a trick to using the immediate window with c++. I'm mostly coming from a C# background, where everything works nicely (and I have intellisense in the Immediate Window). I wasn't expecting C++ to be great, but I figured it would work for things besides ints. Can anyone tell me what I'm doing wrong? Thanks.

EDIT: I should be clear, nothing really works in the immediate window, this is just a simplified example

EDIT: I'm in debug mode

Disproportionate answered 1/2, 2010 at 20:13 Comment(0)
K
15

The immediate and watch windows don't support overloaded operators. There is some support in there for printing standard containers as a whole in a sensible fashion (see, e.g., http://www.virtualdub.org/blog/pivot/entry.php?id=120), but this doesn't extend to being able to use operator[] on them.

Hopefully this will be improved in later revisions of the debugger, but for now, to look at the i'th element of a vector, try lines._Myfirst[i].

(_Myfirst, in the standard libraries that come with VC++, happens to be the member variable in a std::vector that points to the first element of the sequence. So this is just examining a vector as if it were any other object. To work this out, I had to look at the headers... not very convenient, but hopefully this will help you. You can probably do something similar with the other containers, but you'll have to look in the headers to work out how.)

(By the way, if you've been working in C#, the C++ debugger will probably seem by comparison a bit less slick in general, and this is just one example of that. I get the impression there's been much more work put into the CLR side.)

Kingship answered 1/2, 2010 at 20:30 Comment(3)
Calling member functions in the watch and immediate windows just doesn't seem to work either. (I've updated my answer to mention the lack of slickness -- this is a 2nd example!) You can usually call freestanding functions though. Annoying, but you can get used to it -- not that you have any choice of course :) (Hopefully later revisions will improve the native debugging side; it's already perfectly serviceable, but just lacks the polish that the CLR side has.)Kingship
.at() probably fails for the same reason: it's overloaded.Turncoat
this is ridiculous, I might as well just use vscode for C++ developmentEdmondedmonda
S
1

In nowaday's Visual Studio versions (e.g. 2013/2015) _Myfirst member variable does no longer exist for a std::vector variable. Use _C_begin instead - means for the given example use e.g. lines._C_begin[i].

Suburb answered 16/12, 2015 at 12:17 Comment(1)
can you please elaborate your answer or make it more complete with an example? This may help the questioner get a clearer idea of what you mean.Savona

© 2022 - 2024 — McMap. All rights reserved.