TheDoctor
TheDoctor

Reputation: 1530

C++ calling a method outside a class from inside a class

I have a c++ program like this:

class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);

protected:
        virtual void OnControlKeysChanged(uint8_t before, uint8_t after);

    virtual void OnKeyDown  (uint8_t mod, uint8_t key);
    virtual void OnKeyUp    (uint8_t mod, uint8_t key);
    virtual void OnKeyPressed(uint8_t key);
};

//ommitted stuff here

void KbdRptParser::OnKeyPressed(uint8_t key)
{
    keypress(key);

};

void keypress(uint8_t key)
{
    //do stuff...
}

//rest of program...

I want to be able to call keypress from inside KbdRptParser::OnKeyPressed, because there are global variables that won't work if i put the code from keypress into KbdRptParser::OnKeyPressed. How can i accomplish this?

Upvotes: 0

Views: 134

Answers (2)

Srikan
Srikan

Reputation: 2251

You just need to declare it before the

void keypress(unit8_t key);

void KbdRptParser::OnKeyPressed(uint8_t key) {

}

Personal suggestion, and to follow pure Object oriented way, declare keypress as static function of a class and global variables as extern

Upvotes: 2

matiu
matiu

Reputation: 7735

Just need to declare it before you call it.

Add this line at the top, or before the OnKeyPressed implementation:

void keypress(uint8_t key);

That just lets the compiler know, this method doesn't exist yet, but by the time the whole program is linked, it'll be there.

Upvotes: 2

Related Questions