C ++: warning: "..." declared with greater visibility than its field type "... :: <anonymous>"
I get these two warnings (from GCC 4.2 on MacOSX):
/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp: 154: 0 / Users / az / Programmierung / openlierox / build / Xcode /../../ src / main .cpp: 154: warning: 'startMainLockDetector () :: MainLockDetector' is declared with greater visibility than the type of its field 'startMainLockDetector () :: MainLockDetector :: <anonymous>'
/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp: 154: 0 / Users / az / Programmierung / openlierox / build / Xcode /../../ src / main .cpp: 154: warning: "startMainLockDetector () :: MainLockDetector" is declared with more visibility than its basic "Action"
In this code:
struct Action {
virtual ~Action() {}
virtual int handle() = 0;
};
static void startMainLockDetector() {
/* ... */
struct MainLockDetector : Action {
bool wait(Uint32 time) { /* ... */ }
int handle() { /* ... */ }
};
/* ... */
}
I'm not quite sure what these warnings mean (what is visibility?) And how to fix them. (I really want the MainLockDetector class to be local to this function only.)
I already compiled the same code with many other compilers (clang, GCC 3. *, GCC 4.0, GCC 4.4, etc.) and never received any warning for this code.
To resolve this issue, try one of the following.
Use instructions
#pragma GCC visibility push()like this.#pragma GCC visibility push(hidden) struct MainLockDetector : Action { bool wait(Uint32 time) { /* ... */ } int handle() { /* ... */ } }; #pragma GCC visibility popUse
__attribute__ ((visibility("hidden")))as follows.struct __attribute__ ((visibility("hidden"))) MainLockDetector : Action { bool wait(Uint32 time) { /* ... */ } int handle() { /* ... */ } };Add the command line parameter -fvisibility = default.
, .
struct MainLockDetector : public Action {
bool wait(Uint32 time) { /* ... */ }
int handle() { /* ... */ }
};
This causes the members of the Action to be private. But you just redefined the private member of Action as public (public default in the structure), which could violate the encapsulation, hence the warning.