Go Down

Topic: [Solved] Static member of a class impossible?! [Possible!] (Read 531 times) previous topic - next topic

liudr

Mar 11, 2011, 06:03 am Last Edit: Mar 11, 2011, 06:28 am by liudr Reason: 1
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.

AlphaBeta

They're possible!
Code: [Select]

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

//implementation
int Test::x = 0;


Nick Gammon


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.

Quote

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:

Code: [Select]
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.
http://www.gammon.com.au/electronics

liudr

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 :)

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

Thanks again.

Go Up