invoking functions while debugging with Visual Stu

2019-06-16 11:34发布

Here's something I know is probably possible but I've never managed to do
In VS2005(C++), While debugging, to be able to invoke a function from the code which I'm debugging.
This feature is sometimes essential when debugging complex data structures which can't be explored easily using just the normal capabilities of the watch window.
The watch window seem to allow writing function calls but every time I try it it gives me one error or another.

Error: symbol "func" not found
Error: argument list does not match function
Error: member function not present

Did anyone ever succeed in making this work properly? What am I missing here?

Edit: clearly, the function called should be a symbol that exists in the current scope the debugger is in.

7条回答
我命由我不由天
2楼-- · 2019-06-16 12:07

The watch window is limited by the context wherein your current code is, e.g., when your code enters a function and you try to access another function that is hidden from the scope of your current function, it won't work.

If you invoke a function in the watch window, make sure that it is visible and accessible from the current scope.

查看更多
Bombasti
3楼-- · 2019-06-16 12:08

In my experience, there are some shortcomings with the immediate window. You can't call your classes' member functions if the classes come from a different DLL, but get misleading error messages. If anything is in the same DLL (e.g. by statically linking in all other stuff), calling members is fairly reliable. But complex stuff may or may not work, as mentioned by others.

查看更多
神经病院院长
4楼-- · 2019-06-16 12:16

Ok, Here's what I found
CXX0040 means that "The C expression evaluator does not support implicit conversions involving constructor calls."
CXX0047 means that "Overloaded functions can be called only if there is an exact parameter match or a match that does not require the construction of an object."

So combined it means that If I want to call a function none of the arguments should have an implicit conversion and none of the arguments should need a construction.
"implicit conversion" in this context seem to include trivial things like converting 'String' to 'const String&'.
"construction" seem to include trivial copy-construction. so passing by value anything that is not a primitive type will result in an error.

So this basically leaves functions that take only primitive types or pointers.
I have just tested this theory successfully.

So if you want to be able to call a method from the watch window, add an overload which takes only pointers and primitives and in the watch window pass the arguments appropriately. To pass an object that is not a primitive pass its address.

查看更多
Explosion°爆炸
5楼-- · 2019-06-16 12:17

It's the "Immediate" window that you want. And you're limited to what's visible from where your current breakpoint is. Local variables, and functions on that class (or globals)

查看更多
姐就是有狂的资本
6楼-- · 2019-06-16 12:25

To my knowledge, you can't execute code from the Watch window while debugging unmanaged C++. This does work for C# (and probably VB.NET and managed C++, but I'm not positive on that). So likely it allows it because it works for some languages, but not others.

查看更多
Luminary・发光体
7楼-- · 2019-06-16 12:27

I haven't tested this, but I always thought that was what the immediate window was for (executing code)

Cameron

查看更多
登录 后发表回答