【问题标题】:Error: initializer must be brace-enclosed错误:初始化程序必须用大括号括起来
【发布时间】:2022-01-07 16:33:17
【问题描述】:

这个错误是什么意思,为什么我不能用花括号初始化列表初始化这个结构?不幸的是,结构是自动生成的。

// Contains no functionality, purely documentative.
struct NativeTable {};

...

struct TableKeyT : public flatbuffers::NativeTable {
  typedef TableKey TableType;
  std::string exp{};
  std::string type{};
  std::string ext{};
};

...

TableKeyT key { std::string(sym), std::string(ex), std::string("") };
[build] ../../../src/io/socket.cpp:102:39: error: initializer for ‘flatbuffers::NativeTable’ must be brace-enclosed
[build]   102 | TableKeyT key { std::string(sym), std::string(ex), std::string("") };
[build]       |           ^~~

【问题讨论】:

    标签: c++ gcc c++20


    【解决方案1】:

    由于TableKeyT继承了NativeTable,所以还需要初始化基类,不过既然是空类,使用{}应该没问题。

    TableKeyT key { {}, std::string(sym), std::string(ex), std::string("") };
                 //^^^
    

    【讨论】:

    • 谢谢!我不知道您可以将基类初始化程序作为初始化列表中的第一个参数传递。 +1
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2013-10-09
    • 2012-11-21
    • 2021-01-30
    相关资源
    最近更新 更多