![](/img/trans.png)
[英]error: non-aggregate type 'Circle' cannot be initialized with an initializer list
[英]Struct inheritance causing non-aggregate type error on static initialization
我目前有以下內容:
struct MsgHeader_t
{
int a;
float b;
};
它將默認 const 初始化為
const MsgHeader_t default = { 0, 0.0f };
這工作正常。 現在我必須擴展它,它必須從另一個結構繼承。 所以現在我有了。
struct MsgId_t
{
unsigned int id;
};
struct MsgHeader_t : public MsgId_t
{
int a;
float b;
};
但是現在當我嘗試初始化默認的 const ( const MsgHeader_t aMessage = { 0, 0, 0.0f };
) 我得到
error: braces around initializer for non-aggregate type 'const MsgHeader_t'
一旦你有了 inheritance,那么你的 class 就不能再是聚合類型了。 從此草案 C++ 標准(粗斜體字):
8.5.1 聚合
1聚合是一個數組或 class(第 9 條),沒有用戶提供的構造函數(12.1),沒有私有或受保護的非靜態數據成員(第 11 條),沒有基類(第 10 條),也沒有虛函數( 10.3)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.