Search code examples
c++visual-studiodebuggingwatchimmediate-window

Call C++ function inside VS watch or immediate window


Question is fairly simple: Is there a way to call a C++ function from watch window of visual studio, when that function is declared in File1.hpp, defined in File1.cpp and the debugger is currently stopped with a breakpoint in AnotherFile.cpp?

File1.hpp

int &getValue();

File1.cpp

int &getValue()
{
    static int value = 0;
    return value;
}

AnotherFile.cpp

int main()
{
    int x = 0; //Debugger is stopped here, and in watch window of VS i want to call getValue() of 
               //File1.hpp, to check the result
}

This example is simplified. When I call getValue() and the debugger is stopped in File1.cpp, the value is shown in watch window of Visual Studio correctly. When I call getValue() and debugger is stopped in AnotherFile.cpp. I get identifier getValue(void) is undefined


Solution

  • I suggest that you could use Immediate in Debug->Windows->Immediate. enter image description here

    Then, when you debug int x=0, you could input getValue() in Immediate Window. And you will see the console output value 0.

    enter image description here