共用方式為


編譯器錯誤 C2653

'identifier' : 不是類別或命名空間名稱

語言語法在這裡需要類別、結構、等位或命名空間名稱。

當您在範圍運算子前面使用尚未宣告為類別、結構、等位或命名空間的名稱時,就會發生此錯誤。 若要修正此問題,請在使用名稱之前,宣告名稱或包含宣告名稱的標頭。

如果您嘗試定義複合命名空間、包含一或多個範圍巢狀命名空間名稱的命名空間,也可以使用 C2653。 C++17 之前,C++不允許複合命名空間定義。 當您指定 /std:c++latest 編譯程序選項時,Visual Studio 2015 Update 3 支援複合命名空間。 從 Visual Studio 2017 15.5 版開始,編譯程式支援 ](.) 時的 [/std:c++17複合命名空間定義。/..已指定 /build/reference/std-specify-language-standard-version.md) 或更新版本選項。

範例

此範例會產生 C2653,因為會使用範圍名稱,但未宣告。 編譯程式預期範圍運算符 (::)) 之前會有類別、結構、等位或命名空間名稱。

// C2653.cpp
// compile with: /c
class yy {
   void func1(int i);
};

void xx::func1(int m) {}   // C2653, xx is not declared
void yy::func1(int m) {}   // OK

在未針對 C++17 或更新版本標準編譯的程式代碼中,巢狀命名空間必須在每個巢狀層級使用明確的命名空間宣告:

// C2653b.cpp
namespace a::b {int i;}   // C2653 prior to Visual Studio 2015 Update 3,
                          // C2429 thereafter. Use /std:c++17 or /std:c++latest to fix.

namespace a {             // Use this form for conformant code under /std:c++14 (the default)
   namespace b {          // or when using compilers before Visual Studio 2015 update 3.
      int i;
   }
}

int main() {
   a::b::i = 2;
}