non-aggregates cannot be initialized with initializer list

 我定義了一個結構體,示意如下:

Struct A
{
    int x;
    CString test;
};


然後我定義一個變量同時對其進行串行初始化:

A a = {0, "hello"};

編譯出現錯誤:non-aggregates cannot be initialized with initializer list

後來發現,可進行串行初始化的數據結構中是不能夠有construct、private、protected等且沒有基類的聯合體、結構體、類。其成員也必須符合這樣的條件。

編譯出錯是因爲,A結構中成員“test”是CString類型,有構造函數。

以下結構可以進行initializer list,

class c1assDemo
{
public:
    int x;
    char c;
};

 

struct A
{
    int x;
    char test[256];
    c1assDemo demo;
};



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章