編譯器錯誤 C2143
語法錯誤:在 'token2' 之前遺漏 'token1'
編譯程式預期會有特定令牌(也就是空格符以外的語言專案),並改為找到另一個令牌。
請檢查C++語言參考,以判斷程式代碼語法不正確的位置。 因為編譯程式可能會在遇到造成問題的行之後回報此錯誤,所以請檢查錯誤前面的數行程序代碼。
C2143 可能會在不同的情況下發生。
這個錯誤可能會發生在可以限定名稱的運算子 (::
、->
和 .
) 後面必須接著關鍵字 template
,如下列範例所示:
class MyClass
{
template<class Ty, typename PropTy>
struct PutFuncType : public Ty::PutFuncType<Ty, PropTy> // error C2143
{
};
};
根據預設,C++ 假設 Ty::PutFuncType
不是範本;因此下列 <
會解譯成小於符號。 您必須明確地告知編譯器 PutFuncType
為範本,以便它可以正確地剖析角括號。 若要更正這個錯誤,請在相依類型名稱上使用 template
關鍵字,如下所示:
class MyClass
{
template<class Ty, typename PropTy>
struct PutFuncType : public Ty::template PutFuncType<Ty, PropTy> // correct
{
};
};
使用 /clr 且 using
指示詞發生語法錯誤時,可能會發生 C2143:
// C2143a.cpp
// compile with: /clr /c
using namespace System.Reflection; // C2143
using namespace System::Reflection;
當您嘗試使用 CLR 語法編譯原始程式碼檔案而不使用 /clr 時,也可能會發生此問題:
// C2143b.cpp
ref struct A { // C2143 error compile with /clr
void Test() {}
};
int main() {
A a;
a.Test();
}
接在 if
陳述式後面的第一個非空白字元必須為左括號。 編譯程式無法轉譯任何其他專案:
// C2143c.cpp
int main() {
int j = 0;
// OK
if (j < 25)
;
if (j < 25) // C2143
}
偵測到錯誤的那一行或之前其中一行遺漏了右大括號、括號或分號時,C2143 就會發生:
// C2143d.cpp
// compile with: /c
class X {
int member1;
int member2 // C2143
} x;
或是在類別宣告中有無效的標記時:
// C2143e.cpp
class X {
int member;
} x;
class + {}; // C2143 + is an invalid tag name
class ValidName {}; // OK
或當標記未附加至陳述式。 如果您必須單獨放置標籤,例如,在複合語句的結尾處,請將它附加至 null 語句:
// C2143f.cpp
// compile with: /c
void func1() {
// OK
end1:
;
end2: // C2143
}
當對 C++ 標準連結庫中的類型進行未限定呼叫時,就會發生此錯誤:
// C2143g.cpp
// compile with: /EHsc /c
#include <vector>
static vector<char> bad; // C2143
static std::vector<char> good; // OK
或有遺漏 typename
關鍵字:
// C2143h.cpp
template <typename T>
struct X {
struct Y {
int i;
};
Y memFunc();
};
template <typename T>
X<T>::Y X<T>::memFunc() { // C2143
// try the following line instead
// typename X<T>::Y X<T>::memFunc() {
return Y();
}
或者如果您嘗試定義明確具現化:
// C2143i.cpp
// compile with: /EHsc /c
// template definition
template <class T>
void PrintType(T i, T j) {}
template void PrintType(float i, float j){} // C2143
template void PrintType(float i, float j); // OK
在 C 程式中,變數必須在函式開頭宣告,而且無法在函式執行非宣告指令之後宣告變數。
// C2143j.c
int main()
{
int i = 0;
i++;
int j = 0; // C2143
}