C++:警告:'...'声明的可见性比其字段的类型更高'...::'
我收到这两个警告(在 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: warning: '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 等)编译了相同的代码,并且从未收到任何警告对于这段代码。
I'm getting these two warnings (with 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' 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' declared with greater visibility than its base '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 exactly sure what these warnings mean (what visibility?) and how to fix them. (I really want the class MainLockDetector to be local for that function only.)
I have already compiled the same code with a lot of other compilers (clang, GCC 3.*, GCC 4.0, GCC 4.4, etc) and never got any warning for this code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要解决此问题,请尝试以下其中一项。
像这样使用
#pragma GCC Visibility Push()
语句。像这样使用
__attribute__ ((visibility("hidden")))
。添加命令行选项-fvisibility=default。
有关更多详细信息,请参阅 http://gcc.gnu.org/wiki/Visibility。
To fix this problem, try one of below.
Use
#pragma GCC visibility push()
statement like this.Use
__attribute__ ((visibility("hidden")))
like this.Add the command line option -fvisibility=default.
For more details, refer http://gcc.gnu.org/wiki/Visibility.
共享库似乎有问题。我假设您正在编写一个共享库。请参阅此说明。尝试添加命令行选项
-fvisibility-inlines-hidden
。问题似乎是,gcc 尝试导出MainLockDetector
的一些符号(对于链接可执行文件和库可见),而Action
未导出(对于链接可执行文件和库不可见) )。因此,MainLockDetector
的可见性确实高于Action
的可见性。There seems to be a problem with shared libraries. I assume you are writing a shared library. Look at this explanation. Try to add the command line option
-fvisibility-inlines-hidden
. The problem seems to be, that gcc tries to export some symbols ofMainLockDetector
(visible to linking executables and libraries), whereasAction
is not exported (invisible to linking executables and libraries). So the visibility ofMainLockDetector
really is higher than the visibility ofAction
.这是因为您忘记将继承声明为公共。
这导致“Action”成员是私有的。但是,您刚刚将 Action 私有成员重写为公共成员(结构中的公共默认成员),这可能会破坏封装,因此会出现警告。
It's because you forgot to declare the inheritance as public.
This causes the "Action" members to be private. But, you've just overridden an Action private member as public (public default in a struct), which could break encapsulation, hence the warning.