Compiler Error C2552
" identifier」:非彙總無法使用初始設定式清單
彙總識別項未正確初始化。
彙總 定義如下:
陣列
類別、沒有的結構和等位:
建構函式
private 或 protected 成員
基底類別
虛擬函式
此外, Visual C++ 不提供該彙總的資料型別包含建構函式。
下列代表 C2552 可能引發的原因彙總初始化時在型別嘗試:
該型別具有一個或多個使用者定義的建構函式。
這個型別更有礦個非靜態,私用資料成員。
該型別具有一個或多個虛擬函式。
這個型別的基底類別。
這個型別是 ref 類別或 CLR 介面。
這個型別是純量 (int i = {};)
這個型別的項目具有解構函式的非固定維度陣列 (零陣列)。
下列範例會產生 C2552:
// C2552.cpp
// compile with: /clr
#include <string>
using namespace std;
struct Pair_Incorrect {
private:
string m_name;
double m_val;
};
struct Pair_Correct1 {
public:
Pair_Correct1(string name, double val)
: m_name(name), m_val(val) {}
private:
string m_name;
double m_val;
};
struct Pair_Correct2 {
public:
string m_name;
double m_val;
};
int main() {
// To fix, add a constructor to this class and use it for
// initializing the data members, see Pair_Correct1 (below)
// or
// Do not have any private or protected non-static data members,
// see Pair_Correct2 (below). Pair_Correct2 is not recommended in
// case your object model requires some non-static data members to
// be private or protected
string name("John");
Pair_Incorrect pair1 = { name, 0.0 }; // C2552
// initialize a CLR immutable value type that has a constructor
System::DateTime dt = {2001, 4, 12, 22, 16, 49, 844}; // C2552
Pair_Correct1 pair2( name, 0.0 );
Pair_Correct1 pair3 = Pair_Correct1( name, 0.0 );
Pair_Correct2 pair4 = { name, 0.0 };
System::DateTime dt2(2001, 4, 12, 22, 16, 49, 844);
}