C++:警告:'...'声明的可见性高于其字段'... :: <anonymous>'的类型

Alb*_*ert 7 c++ gcc warnings visibility gcc-warning

我收到了这两个警告(在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 :: <anonymous>'的类型

/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() { /* ... */ }
    };

    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

我不确定这些警告意味着什么(能见度?)以及如何解决它们.(我真的希望类MainLockDetector只对该函数是本地的.)

我已经与许多其他编译器(clang,GCC 3.*,GCC 4.0,GCC 4.4等)编译了相同的代码,并且从未对此代码发出任何警告.

Sat*_*ito 6

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

  1. 使用#pragma GCC visibility push()这样的声明。

    #pragma GCC visibility push(hidden)
    struct MainLockDetector : Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
    #pragma GCC visibility pop
    
    Run Code Online (Sandbox Code Playgroud)
  2. __attribute__ ((visibility("hidden")))像这样使用。

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

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


Pup*_*ppy -6

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

    struct MainLockDetector : public Action {
         bool wait(Uint32 time) { /* ... */ }
         int handle() { /* ... */ }
    };
Run Code Online (Sandbox Code Playgroud)

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

  • 这对我来说毫无意义。正如您所说的“结构中的公共默认值”,因此“公共”是可选的(它是成员和继承的默认值)。 (2认同)