將 'declaration' 移至匿名命名空間,或在這個檔案中包含的通用標頭中提出宣告。
備註
這項檢查旨在協助減少特定符號的可見度,以及將程式代碼模組化。 在多檔案C++專案中,每個宣告都應該是本機C++檔案(匿名命名空間的一部分),或宣告在多個C++檔案所包含的通用頭檔中。
當此檢查標幟宣告時,它應該移至匿名命名空間,或應該根據符號的範圍,將正向宣告移至頭檔。
此規則是必須在規則集檔案中明確啟用才能運作的實驗性規則。 如需規則集的詳細資訊,請參閱 使用規則集將程式代碼分析規則分組。
程式碼分析名稱:MARK_INTERNAL_OR_MISSING_COMMON_DECL
範例
下列範例會產生 C6389:
// A.h
struct X;
// A.cpp
#include "A.h"
// Not flagged, declared in a header file.
struct X { int x; };
struct Y { double y; }; // warning: Move 'Y' to anonymous namespace or put a forward declaration in a common header included in this file.
void f(); // warning: Move 'f' to anonymous namespace or put a forward declaration in a common header included in this file.
解決這些問題的其中一種方法是移至 struct Y 匿名命名空間,並將的宣告 f 移至標頭:
// A.h
struct X;
void f();
// A.cpp
#include "A.h"
// Not flagged, declared in a header file.
struct X { int x; };
namespace {
struct Y { double y; };
} // anonymous namespace
// Not flagged, declared in a header file.
void f();