1. 程式人生 > >Mr.J--C語言編譯錯誤C2039

Mr.J--C語言編譯錯誤C2039

編譯器錯誤 C2039

identifier1: 不是 identifier2 的成員

程式碼錯誤地呼叫或引用的結構、 類或聯合成員。

示例

下面的示例生成 C2039。

複製

// C2039.cpp
struct S {
   int mem0;
} s, *pS = &s;

int main() {
   pS->mem1 = 0;   // C2039 mem1 is not a member
   pS->mem0 = 0;   // OK
}

示例

下面的示例生成 C2039。

複製

// C2039_b.cpp
// compile with: /clr
using namespace System;
int main() {
   Console::WriteLine( "{0}", DateTime::get_Now());   // C2039
   Console::WriteLine( "{0}", DateTime::Now);   // OK
   Console::WriteLine( "{0}", DateTime::Now::get());   // OK
}

示例

下面的示例生成 C2039。

複製

// C2039_c.cpp
// compile with: /clr /c
ref struct S {
   property int Count {
     int get();
     void set(int i){}
   };
};

int S::get_Count() { return 0; }   // C2039
int S::Count::get() { return 0; }   // OK

示例

如果您嘗試訪問預設索引器不正確,也可能發生 C2039。 下面的示例定義 C# 編寫的元件。

複製

// C2039_d.cs
// compile with: /target:library
// a C# program
[System.Reflection.DefaultMember("Item")]
public class B {
   public int Item {
      get { return 13; }
      set {}
   }
};

示例

下面的示例生成 C2039。

複製

// C2039_e.cpp
// compile with: /clr
using namespace System;
#using "c2039_d.dll"

int main() {
   B ^ b = gcnew B;
   int n = b->default;   // C2039
   // try the following line instead
   // int n = b->Item;
   Console::WriteLine(n);
}

示例

如果使用泛型,也可能發生 C2039。 下面的示例生成 C2039。

複製

// C2039_f.cpp
// compile with: /clr
interface class I {};

ref struct R : public I {
   virtual void f3() {}
};

generic <typename T>
where T : I
void f(T t) {
   t->f3();   // C2039
   safe_cast<R^>(t)->f3();   // OK
}

int main() {
   f(gcnew R());
}

示例

當你嘗試釋放託管或非託管資源,則會發生 C2039。 有關詳細資訊,請參閱解構函式和終結器

下面的示例生成 C2039。

複製

// C2039_g.cpp
// compile with: /clr
using namespace System;
using namespace System::Threading;

void CheckStatus( Object^ stateInfo ) {}

int main() {
   ManualResetEvent^ event = gcnew ManualResetEvent( false );
   TimerCallback^ timerDelegate = gcnew TimerCallback( &CheckStatus );
   Timer^ stateTimer = gcnew Timer( timerDelegate, event, 1000, 250 );

   ((IDisposable ^)stateTimer)->Dispose();   // C2039

   stateTimer->~Timer();   // OK
}