anisha@linux-dopx:~/> gdb a.out
(gdb) watch dummyA::x
Cannot reference non-static field "x"
(gdb)
x is a private member of a class named dummyA.
How to set a watch point on the private member of the class?
Language: C++
Platform: Linux
EDIT 1:
#include <iostream>
using namespace std;
class dummyA
{
int x;
public:
dummyA ()
{
x = 0;
}
void test ()
{
x++;
}
};
int main ()
{
dummyA obj;
obj.test ();
obj.test ();
obj.test ();
}
Output:
(gdb) watch obj.x
No symbol "obj" in current context.
(gdb) watch obj::x
No symbol "obj" in current context.
Now, what does that error mean?
Suppose you have this:
Now you have two instances of
Anamedfooandbar. If you tell the debugger to watchA::xhow does it know which instance you mean?When you watch an instance variable (of which there is one for each instance) instead of a static variable (of which there is only one for every class) you need to specify which instance’s variable you want to watch. You are specifying which class’ variable you want to watch. And while that would be OK with a static variable (there is only one static variable per class) it’s not OK with an instance variable.
In this case, in
main, after stepping past the two linesA foo;andA bar;you could do:or
and it would work just fine. You have to step past those lines because not even the names (much less the objects they refer to) exist until after them.