views:

343

answers:

2

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?

+1  A: 

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.
Daniel Daranas
I cannot remember, is there a native assert operator in C++? Or must one devise their own?Moreover, do you think it would work to write wrapper functions as interfaces to commonly inspected expressions to work-around this debugger limitation? That incurs a little more programming overhead, but I cannot think of a better approach just yet.
Jonny
@Jonny assert is a macro. It is typically defined by the compiler vendor. And yes, writing extra functions would help clarify what's going on.
Daniel Daranas
+1  A: 

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.

Sebastian
the watch window does execute code
stijn
@stijn: Can you elaborate? It does evaluate expressions, i.e., you can watch "(a+b)/2", it does evaluate code to format expressions following rules given in autoexp.dat, but when does it execute my code?
Sebastian
@stijn: Updated my post. I found out that you are right, calling functions is allowed in the watch window. I didn't know that.
Sebastian
I think I recall seeing overloaded operator functions being evaluated in the watch window "once in a blue moon" but most often they definitely do not evaluate.
Jonny