Share via


編譯器錯誤 C2512

'identifier' : 沒有適當的預設建構函式可用

指定的類別、結構或等位沒有可用的預設建構函式。 如果沒有提供使用者定義的建構函式,則編譯器會提供一個預設的建構函式。

如果您提供使用非 void 參數的建構函式,而且您要讓類別在沒有參數下建立,則您也必須提供預設建構函式。 預設建構函式可以是一個所有參數都使用預設值的建構函式。

下列範例會產生 C2512:

// C2512.cpp
// C2512 expected
struct B {
   B (char *);
   // Uncomment the following line to resolve.
   // B() {};
};

int main() {
   B b; 
}

下列範例會示範更加奧妙的 C2512:

// C2512b.cpp
// compile with: /c
struct S {
   struct X;

   void f() {
      X *x = new X();   // C2512 X not defined yet
   }

};

struct S::X {};

struct T {
   struct X;
   void f();
};

struct T::X {};

void T::f() {
   X *x = new X();
}