代码改变世界

vs 在watch监视窗口 vector 错误 :overloaded operator not found

2012-06-15 13:42  youxin  阅读(1201)  评论(0编辑  收藏  举报

 一下的代码,debug显示是正常的。如果你调试时,查看watch窗口会发现一个错误:

overloaded operator not found找不到重载运算符

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    vector<vector<string> > vec;
    vector<string> vec1,vec2;
    vec1.push_back("can");
    vec1.push_back("you");
    vec2.push_back("speak");
    vec2.push_back("english");
    vec.push_back(vec1);
    vec.push_back(vec2);

    cout<<vec[0][0]<<endl;
}

 

原因:

Codeview 的表达式计算器要求精确的参数匹配来评估重载的函数。此外的参数匹配,最高级别修饰符 (即,const,易失性) 将被忽略。

The errors you have are due to limitations in the debugger, there are not bugs as Daniel implies.

The watch window cannot call overloaded operators. If you have e.g. a std::vector<int> vecSomethingyou cannot put vecSomething[0] into the watch window, because std::vector<int>::operator[] is an overloaded operator. Consequently, for a vector of objects, you cannot dovecObject[0].SomeMemberVariableOfObject in the watch window. You could writevecObject._Myfirst[0].SomeMemberVariableOfObject. In Visual Studio's STL implementation,_Myfirst is a member of vector pointing at the first element.

If you add your own variables and types to the watch window, add watches to the data members directly. It is no problem to follow chains of pointers like member.memberStruct.ptrToObj->memberOfObj.

 

总之,加上_Myfirst即可。

vec._Myfirst[0]._Myfirst[0]

vec._Myfirst[1]._Myfirst[1]

 

参考了:

http://support.microsoft.com/kb/116370

http://stackoverflow.com/questions/2179724/why-cant-i-index-a-stdvector-in-the-immediate-window