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;
}

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.