[Solved] Static member of a class impossible?! [Possible!]

I'm revising my buttons class and thought it's cool to add a static variable:
static unsigned long t_last;
Compiler is not happy:

Error:
undefined reference to `buttons::t_last'

So, is the entire arduino code wrapped around in a class so all the classes I define are member classes thus can't have static members?
Same fate as the Processing?

Can someone clarify about static members (data and function) in general?

Thanks!

Update: I looked at the source codes and the main.cpp in arduino. It doesn't seem that the entire code is wrapped in one class def. so what could be the reason?

I changed my variable name just in case some internal variable has the same name. Still no go.

They're possible!

//header
class Test {
  static int x;
};

//implementation
int Test::x = 0;

liudr:
So, is the entire arduino code wrapped around in a class so all the classes I define are member classes thus can't have static members?

No.

Can someone clarify about static members (data and function) in general?

It helps when you have a compiler error to post the code in question, it helps explain. Static variables belong to the class, but not to an instance of the class. So in the example posted above:

static int x;

There is one "x" shared between all instances of the class (or indeed, even if no instances exist). A "normal" variable belongs to the instance. So every time you create a new instance of that class you get another copy of the variable.

Ditto for static functions. Normal functions "get" access to the variables in the instance to which they belong. Static functions do not have access to class variables because they do not belong to any instance of the class. However static functions might usefully use static variables.

AlphaBeta, thanks for trying. So it does exist. Kudos to real C++. I remember hitting a hard wall when trying to use static variables in Processing.

Nick, Thanks for the explanation. I posted my error message. I should have read a bit more into the message. It says undefined variable.

After some research into the syntax of static variables, it turns out that I needed to define the variable like a regular global variable with:

unsigned long buttons::t_last_action=0;

in my buttons.cpp

I thought if I declare a variable to be static, I don't have to define or initialize it (stupid thoughts). I did. I just have to define the variable and everything works nicely from there. My little world of OO is safe XD.

FYI, the variable tracks the last key pad activity on my Phi-1 shield, so if someone wants to program a sleep code, they can just read this variable and decide :slight_smile:

Always trying to do something that someone may in the distance future benefit from. That's what us physicists always do :grin:

Thanks again.