Pages: [1]   Go Down
Author Topic: [Solved] Static member of a class impossible?! [Possible!]  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 11, 2011, 12:28:54 am by liudr » Logged


Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

They're possible!
Code:
//header
class Test {
  static int x;
};

//implementation
int Test::x = 0;
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18798
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-lol.

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 smiley

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.
Logged


Pages: [1]   Go Up
Jump to: