C++:警告:'...'声明的可见性比其字段的类型更高'...::'

发布于 2024-09-01 14:32:40 字数 1014 浏览 12 评论 0原文

我收到这两个警告(在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

憧憬巴黎街头的黎明 2024-09-08 14:32:40

要解决此问题,请尝试以下其中一项。

  1. 像这样使用#pragma GCC Visibility Push()语句。

    #pragma GCC 可见性推送(隐藏)
    结构MainLockDetector:操作{
         bool wait(Uint32 time) { /* ... */ }
         int 句柄() { /* ... */ }
    };
    #pragma GCC 可见性流行
    
  2. 像这样使用__attribute__ ((visibility("hidden")))

    struct __attribute__ ((visibility("hidden"))) MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int 句柄() { /* ... */ }
    };
    
  3. 添加命令行选项-fvisibility=default。

有关更多详细信息,请参阅 http://gcc.gnu.org/wiki/Visibility

To fix this problem, try one of below.

  1. Use #pragma GCC visibility push() statement like this.

    #pragma GCC visibility push(hidden)
    struct MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
    #pragma GCC visibility pop
    
  2. Use __attribute__ ((visibility("hidden"))) like this.

    struct __attribute__ ((visibility("hidden"))) MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
    
  3. Add the command line option -fvisibility=default.

For more details, refer http://gcc.gnu.org/wiki/Visibility.

╰ゝ天使的微笑 2024-09-08 14:32:40

共享库似乎有问题。我假设您正在编写一个共享库。请参阅此说明。尝试添加命令行选项-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 of MainLockDetector (visible to linking executables and libraries), whereas Action is not exported (invisible to linking executables and libraries). So the visibility of MainLockDetector really is higher than the visibility of Action.

中性美 2024-09-08 14:32:40

这是因为您忘记将继承声明为公共。

    struct MainLockDetector : public Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };

这导致“Action”成员是私有的。但是,您刚刚将 Action 私有成员重写为公共成员(结构中的公共默认成员),这可能会破坏封装,因此会出现警告。

It's because you forgot to declare the inheritance as public.

    struct MainLockDetector : public Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文