Vector 'no operator “[]” matches these operands' error in Visual Studio watch

谁说胖子不能爱 提交于 2019-12-20 17:25:24

问题


When stepping through the following sample code in Visual Studio 2012:

std::vector<int> test;
test.resize(1);
test[0] = 4;

I can set a watch on test and inspect its 0th element. However, if I set a watch on test[0], I get the error 'no operator "[]" matches these operands':

How can I inspect the value of test[0] directly?


回答1:


I found one solution which does not depend on the internals of the class. The expanded form of the operator call seems to work for me. In this case it's the following code:

v.operator[](0)

I tested it in Visual C++ 2012.




回答2:


As @NateKohl noted, in Visual Studio 2012 (and possibly earlier versions as well) v._Myfirst gives a pointer to the underlying vector data, allowing you to watch the vector as if it were an array.




回答3:


Visual Studio doesn't support stl containers' operator[] overloading, you simply have to manually set a watch on the element that you're interested in by selecting it from the list while debugging.

EDIT: if you want to inspect a T object inside a vector, assign it to a T object and set a watch on it instead




回答4:


if you use 2D vecotr< vector< string > > dp, and you want to get dp[i][j] in watch window in VS2013, you can use (dp.operator [ ] (i)).operator [ ] (j)

vector< vector < string > > dp(n, vector < string >(n, ""));

(dp.operator [ ] (i)).operator [ ] (j)



来源:https://stackoverflow.com/questions/17799464/vector-no-operator-matches-these-operands-error-in-visual-studio-watch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!