我收到这两个警告(在 MacOSX 上使用 GCC 4.2):
/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154:0/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154: 警告:“startMainLockDetector()::MainLockDetector”声明的可见性高于其字段类型“startMainLockDetector()::MainLockDetector::
/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154:0/Users/az/Programmierung/openlierox/build/Xcode/../../src/main.cpp:154: 警告:“startMainLockDetector()::MainLockDetector”声明的可见性高于其基本“Action”
在这段代码中:
struct Action {
virtual ~Action() {}
virtual int handle() = 0;
};
static void startMainLockDetector() {
/* ... */
struct MainLockDetector : Action {
bool wait(Uint32 time) { /* ... */ }
int handle() { /* ... */ }
};
/* ... */
}
我不太确定这些警告的含义(可见性如何?)以及如何修复它们。 (我真的希望 MainLockDetector 类仅针对该函数是本地的。)
我已经用许多其他编译器(clang、GCC 3.*、GCC 4.0、GCC 4.4 等)编译了相同的代码,但从未收到针对此代码的任何警告。
最佳答案
要解决此问题,请尝试以下方法之一。
像这样使用
#pragma GCC visibility push()
语句。#pragma GCC visibility push(hidden) struct MainLockDetector : Action { bool wait(Uint32 time) { /* ... */ } int handle() { /* ... */ } }; #pragma GCC visibility pop
像这样使用
__attribute__ ((visibility("hidden")))
。struct __attribute__ ((visibility("hidden"))) MainLockDetector : Action { bool wait(Uint32 time) { /* ... */ } int handle() { /* ... */ } };
添加命令行选项 -fvisibility=default。
有关详细信息,请参阅 http://gcc.gnu.org/wiki/Visibility .
关于C++:警告: '...' 声明的可见性高于其字段 '...::<anonymous>' 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2828738/