Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Static function causing problems in C++

+4 votes
asked Dec 12, 2021 by Areeb Sherjil (1,960 points)
Hi,

My aim is to call a member function without an object is created, therefore I resorted to using a static function. This function's purpose is to toggle some LEDs and change the frequency of interrupt service routine. Code snippet:

"

class Flags

{

public:

volatile static bool IP_flag;

volatile static bool IP_flag2;

static void changeFreq();

private:

LEDs LED[8] = {LED8,LED6,LED4,LED3,LED5,LED7,LED9,LED10};

};

volatile bool Flags::IP_flag {};

volatile bool Flags::IP_flag2{};// both set to 0/false

void Flags::changeFreq()

{

LED[3].toggle();

LED[7].toggle();// toggle the red LEDs

/*  other code */
}
"

error: invalid use of member 'Flags::LED' in static member function

How to fix this error?

(Note: The full code cannot be provided as it contains large libraries for a microcontroller. )

1 Answer

0 votes
answered Dec 12, 2021 by Peter Minarik (84,720 points)
selected Dec 12, 2021 by Areeb Sherjil
 
Best answer
You're trying to access a member variable (LED) from a static function. Of course, that's not allowed (you don't have an instance reference). From static function only static member variables can be accessed.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...