Cubi73
Cubi73

Reputation: 1951

Private static member not accessilble from public static member function?

Let's say I have following code:

#define STACK_SIZE 5

class Stack {
    private: static int iStackPos;
    private: static void *rgpvStack[STACK_SIZE];

    public: static void Push (
        void *pvElem);
};

int Stack::iStackPos = 0;
void *rgpvStack[STACK_SIZE];

void Stack::Push (void *pvElem) {
    if (Stack::iStackPos < STACK_SIZE) {               // [1]
        Stack::rgpvStack[Stack::iStackPos++] = pvElem; // [2]
    }
}

When I compile this (with AVR-GCC, 03/2012) I get following errors:

I know that these variables are private, because I marked them with private:, because they shoulndn't be accessible outside this class. If I'm not mistaken, Push is a member of Stack, so iStackPos and rgpvStack should be accessible within Push, or not?


EDIT:

The original code has 450 lines, but I summarized it a little bit. The functions loop and setup make use of functions declared in lines with // .... These functions doesn't make any use of my problematic functions and boolean is declared somewhere in the headers of the Arduino Uno board. Actually I saw my (very stupid) mistakes when I was shortening my code and marked them (see below). Thank you anyway :)

#define STACK_SIZE 5

// ...

class Stack;

// ...

class Stack {
    private: static int iStackPos;
    private: static void *rgpvStack[STACK_SIZE];

    public: static boolean Pop (
        void *pvElem);

    public: static boolean Push (
        void *pvElem);
};

// ...

int Stack::iStackPos = 0;
void *Stack::rgpvStack[STACK_SIZE];



boolean Pop (void *pvElem) {   // !!! Must be Stack::Pop !!!
    // TODO
    return false;
}



boolean Push (void *pvElem) {  // !!! Must be Stack::Push !!!
    if (Stack::iStackPos < STACK_SIZE) {
        Stack::rgpvStack[Stack::iStackPos++] = pvElem;
        return true;
    }

    return false;
}

// ...

void loop () {
    // CLI::ProcessSerial();
    // 
    // // Animate LEDs
    // Prog::pAnim->Calculate();
    // 
    // for (int n = 0; n < 5; n++) {
    //     Prog::pMgr->Set(n, Prog::pAnim->pColor);
    // }
    // 
    // Prog::pMgr->Sync();
    // 
    // // Delay
    // delay(BOARD_LOOP_DELAY);
}



void setup () {
    Serial.begin(BOARD_SYMBOL_RATE);
    while (!Serial);

    // Prog::Prog::pMgr = new LedMgr(A0, 5);
    // 
    // Prog::pAnim = new Animation(2000);
    // Prog::pAnim->pfnFinished = Prog::AnimFinished;
    // Prog::pAnim->pInColor = new Color(0, 20, 0);
    // Prog::pAnim->pOutColor = new Color(0, 0, 20);
    // 
    // CLI::ShowHelp();
}

Upvotes: 0

Views: 66

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

This error can occur if you wrote the definition of the function like

void Push (void *pvElem) {
    if (Stack::iStackPos < STACK_SIZE) {               // [1]
        Stack::rgpvStack[Stack::iStackPos++] = pvElem; // [2]
    }
}

instead of

void Stack::Push (void *pvElem) {
    if (Stack::iStackPos < STACK_SIZE) {               // [1]
        Stack::rgpvStack[Stack::iStackPos++] = pvElem; // [2]
    }
}

Take into account that instead of

void *rgpvStack[STACK_SIZE];

there must be

void * Stack::rgpvStack[STACK_SIZE];

Upvotes: 2

Related Questions