Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio fails to display some watched expressions

In Visual Studio, most of my objects and variables cannot be resolved during a debugging session for various reasons. This means I cannot inspect or watch objects or their invoke their functions making it extremely difficult to debug my code because most of my expressions simply won't work. Some typical errors I get when adding an expression to the watch window include:

  • CXX0019: Error: bad type cast
  • CXX0059: Error: left operand is class not a function name
  • CXX0058: Error: overloaded operator not found

Most often these expressions involve overloaded operators and/or template class objects.

Why is this happening? how do you fix it?

like image 597
Coder Guy Avatar asked Nov 24 '09 08:11

Coder Guy


People also ask

How do I enable a watch window in Visual Studio?

To open it, click on the 'Debug' menu item in Visual Studio, point to 'Windows', highlight 'Watch', and then select 'Watch 1' (or any of the other watch windows). An alternative is the Ctrl + Alt + W , 1 key combination.

How do I open the Watch window in Visual Studio 2022?

The Watch Window. The Watch Window allows you to see value of variables and expressions while debugging. It's kind of like the DataTip you get when hovering over a variable, except that you can write any expression you want. It's available from Debug | Windows | Watch | Watch 1 or Ctrl + Alt + W + 1.

Which window is used to display the current Watch expression?

After you enter a watch expression, the Watch window displays the current value of the expression. As your program runs, the value of the watch changes as your program updates the values of the variables in the watch expression.

How do I view quick watch in Visual Studio?

Open and explore the 'QuickWatch' window in Visual Studio That way we can quickly evaluate or change expressions. The 'QuickWatch' window has three columns: 'Name', 'Value', and 'Type'. They show the expression currently evaluated, its value, and type.


2 Answers

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> vecSomething you 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 do vecObject[0].SomeMemberVariableOfObject in the watch window. You could write vecObject._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.

Edit:

Actually Visual Studio can call code in the Watch window: http://geekswithblogs.net/sdorman/archive/2009/02/14/visual-studio-2008-debugging-ndash-the-watch-window.aspx

Thus, it is slightly mysterious why overloaded operators cannot be used.

like image 166
Sebastian Avatar answered Sep 23 '22 01:09

Sebastian


Why is this happening?

The tool has its limitations. For example, many times I "go to definition" and the definition is not found. I have to "find in files". It is no surprise that some expressions are not evaluated during debugging sessions, either.

How do you fix it?

  • Keep expressions simple. Do not concatenate them directly, use variables with explanatory names for intermediate results.
  • Support your code with explicit assertions. If it's "wrong", an assertion should fail.
like image 30
Daniel Daranas Avatar answered Sep 21 '22 01:09

Daniel Daranas