編譯器錯誤 C3293
'accessor': 請用 'default' 存取類別 'type' 的預設屬性 (索引子)
不正確地存取索引屬性。 如需詳細資訊,請參閱 如何:在 C++/CLI 中使用屬性。
Visual Studio 2017 和更新版本:在 Visual Studio 2015 和更早版本中,編譯程式在某些情況下錯誤地將默認屬性誤認為為預設索引器。 使用 "default" 識別碼存取屬性,即可解決問題。 在 C++11 中將 default 引進為關鍵字之後,因應措施本身會變成問題。 因此,在 Visual Studio 2017 中,已修正需要因應措施的 Bug,而且編譯器現在會在使用 "default" 來存取類別的預設屬性時引發錯誤。
範例
下列範例會在 Visual Studio 2015 和更早版本中產生 C3293。
// C3293.cpp
// compile with: /clr /c
using namespace System;
ref class IndexerClass {
public:
// default indexer
property int default[int] {
int get(int index) { return 0; }
void set(int index, int value) {}
}
};
int main() {
IndexerClass ^ ic = gcnew IndexerClass;
ic->Item[0] = 21; // C3293 in VS2015 OK in VS2017
ic->default[0] = 21; // OK in VS2015 and earlier
String ^s = "Hello";
wchar_t wc = s->Chars[0]; // C3293 in VS2015 OK in VS2017
wchar_t wc2 = s->default[0]; // OK in VS2015 and earlier
Console::WriteLine(wc2);
}