How do I solve this?

illegal reference to data member 'myClass:flag' in a static member function

class myClass{

public:
static void change_flag();

private:
bool flag;
};

myClass::change_flag(){
flag=false;
}

Dani AI

Generated

Short answer: the two errors come from different causes that are easy to fix once you understand the rules. A static member function has no this pointer, so it cannot access instance (non‑static) fields directly. And a C API hook (Allegro’s set_window_close_hook) expects a plain C‑style function pointer — a non‑static member function cannot be converted to that because it needs a this. was right that making the flag static lets a static method touch it, but that makes the flag shared by every object. ’s comment about “don’t use static in public” is misleading; access specifiers and the static keyword are orthogonal (see ).

Practical options:

  • Use a single shared flag (make the flag static) if you really want one global state for all instances.
  • Use a simple C-style wrapper function that forwards to an object. Store a pointer to the instance in a file‑scope/static variable, register the wrapper with Allegro and clear/unregister it in the destructor to avoid a dangling pointer. Example pattern:
    
    static MyClass* g_close_target = nullptr;

extern "C" void allegro_close_hook()
{
if (g_close_target) g_close_target->onClose();
}

// register inside object:
// g_close_target = this;
// set_window_close_hook(allegro_close_hook);

- Or keep the pointer inside the class as a static instance pointer and expose a static hook that forwards to that instance:

class MyClass {
static MyClass* s_instance;
void onClose();
static void hook() { if (s_instance) s_instance->onClose(); }
};


- If you can use C++11+, a non‑capturing lambda that calls a global/static target is also convertible to a C function pointer (capturing lambdas are not).

Cautions: always clear the global/static pointer in the destructor and unregister the hook before the object is destroyed, and consider thread‑safety if the hook can run on another thread. For a single window/instance the static‑pointer wrapper is the most practical; for many instances rethink the API (pass a user data pointer) or use a design where a single manager forwards the event.

Recommended Answers

All 6 Replies

public:
static void change_flag();

should that not be void change_flag(); static ?

The static keyword specifies that the function accesses only static members, so make your flag static. Other error will appear, but maybe U can fix it by yourself :)

Don't use static in a public class member. Public means it's accessable to the outside world, but static limits it too the segment of code where you've declared it. If you want to hide it from other peices of code put it in a protected or private section of the class.

>Public means it's accessable to the outside world, but static limits it too the segment of code where you've declared it.
You're mixing up your statics. :) static is overloaded for too many different uses. In a class declaration, static means that the name belongs to the class and not to individual objects. In a function definition, static means that the variable being declared has static storage duration. In the global scope, static is a deprecated feature that forces internal linkage.

public:
static void change_flag();

should that not be void change_flag(); static ?

I'm using Allegro. There's a function there called: set_window_close_hook

class myclass{
public:
static void endLoop();
bool endloop;
};

void myclass::endLoop(){
endloop=true;
}

When I set:
set_window_close_hook(endLoop) ;
I got the error:


cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

When I defined the function as static void endLoop(); in the class it solved the problem.

Ya allegro and member functions don't work well together. Its cause the functions are written for C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.