简体   繁体   English

使用Visual Studio 2005调试时调用函数?

[英]invoking functions while debugging with Visual Studio 2005?

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. 在VS2005(C ++)中,在调试时,能够从我正在调试的代码中调用一个函数。
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. 编辑:显然,调用的函数应该是调试器所在的当前范围中存在的符号。

Ok, Here's what I found 好的,这是我找到的
CXX0040 means that " The C expression evaluator does not support implicit conversions involving constructor calls. " CXX0040意味着“ 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. " CXX0047意味着“ 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&' . 在这种情况下,“隐式转换”似乎包括将'String'转换为'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. 传递非原始对象传递其地址。

The watch window is limited by the context wherein your current code is, eg, 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. 如果在监视窗口中调用某个函数,请确保它在当前作用域中可见且可访问。

To my knowledge, you can't execute code from the Watch window while debugging unmanaged C++. 据我所知,在调试非托管C ++时,您无法从Watch窗口执行代码。 This does work for C# (and probably VB.NET and managed C++, but I'm not positive on that). 这适用于C#(可能还有VB.NET和托管C ++,但我对此并不乐观)。 So likely it allows it because it works for some languages, but not others. 很可能它允许它,因为它适用于某些语言,但不适用于其他语言。

We find this works in a very hit and miss manner. 我们发现这种方式非常受欢迎。 Some very simple functions (incl. member functions) work, typically simple property getters. 一些非常简单的函数(包括成员函数)工作,通常是简单的属性getter。 Other more complex functions don't work and give an error. 其他更复杂的功能不起作用并产生错误。

I've never been able to discern the precise rules ... 我从来没有能够辨别出精确的规则......

I haven't tested this, but I always thought that was what the immediate window was for (executing code) 我没有对此进行测试,但我一直认为这就是即时窗口(执行代码)

Cameron 卡梅伦

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) 局部变量和该类(或全局变量)上的函数

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. 如果类来自不同的DLL,则不能调用类的成员函数,但会得到误导性的错误消息。 If anything is in the same DLL (eg by statically linking in all other stuff), calling members is fairly reliable. 如果任何东西在同一个DLL中(例如通过静态链接所有其他东西),调用成员是相当可靠的。 But complex stuff may or may not work, as mentioned by others. 但其他人提到,复杂的东西可能会或可能不会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM