コンパイラの警告 (レベル 2) C5046

'function': 内部リンケージがある型を含むシンボルは定義されていません

解説

コンパイラは、定義を持たない関数の使用を検出しましたが、この関数のシグニチャには、この翻訳単位の外部に表示されない型が含まれています。 これらの型は外部からは見えないため、他の翻訳単位はこの関数の定義を提供できず、プログラムを正常にリンクできません。

翻訳単位間で表示されない型は次のとおりです。

  • 匿名の名前空間内で宣言された型

  • ローカル クラスまたは名前のないクラス

  • これらの型をテンプレート引数として使用するテンプレートの特殊化。

この警告は Visual Studio 2017 バージョン 15.8 で新たに追加されたものです。

このサンプルでは、2 つの C5046 警告を示します。

// C5046p.cpp
// compile with: cl /c /W2 C5046p.cpp

namespace {
    struct S {
        // S::f is inside an anonymous namespace and cannot be defined outside
        // of this file. If used, it must be defined somewhere in this file.
        int f();
    };
}

// g has a pointer to an unnamed struct as a parameter type. This type is
// distinct from any similar type declared in other files, so this function
// cannot be defined in any other file.
// Note that if the struct was declared "typedef struct { int x; int y; } S, *PS;"
// it would have a "typedef name for linkage purposes" and g could be defined
// in another file that provides a compatible definition for S.

typedef struct { int x; int y; } *PS;
int g(PS p);

int main()
{
    S s;
    s.f();      // C5046 f is undefined and can't be defined in another file.
    g(nullptr); // C5046 g is undefined and can't be defined in another file.
}

これらの問題を解決するには、このファイル内の関数を定義します。