Initialization of const data members per constructor member-initializer list, error: no matching function for call
Initialization of const data members per constructor member-initializer list, error: no matching function for call
我关注class:
Foo.h
class Foo {
public:
struct Bus {
SPIClass *spi;
const uint8_t pin;
};
constexpr Foo(SPIClass *spi, uint8_t csPin) :
_bus({.spi{spi}, .pin{csPin}}) {}
protected:
const Bus _bus;
};
因为 const 成员 _bus 我在构造函数中使用初始化列表对它们进行了初始化。
但不知何故无法识别它并抛出以下编译错误:
error: no matching function for call to 'Foo::Bus::Bus(<brace-enclosed initializer list>)'
_bus({.spi{spi}, .pin{csPin}}) {}
^
note: candidate: 'constexpr Foo::Bus::Bus(const TMC::Motionctrl::Bus&)'
struct Bus {
^~~
note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const Foo::Bus&'
note: candidate: 'constexpr Foo::Bus::Bus(Foo::Bus&&)'
note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'Foo::Bus&&'
error: member 'Foo::_bus' must be initialized by mem-initializer in 'constexpr' constructor
_bus({.spi{spi}, .pin{csPin}}) {}
^
我猜第二个错误是因为初始化失败,但是我找不到错误。
我正在使用 GCC(GNU Arm 嵌入式工具链),构建命令参数是
\packages\STM32\tools\arm-none-eabi-gcc.2.1-1.7/bin/arm-none-eabi-g++" -mcpu=cortex-m3 -mthumb "@E:\xxx\Release/sketch/build_opt.h" -c -Os -Wall -Wextra -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD "-
谢谢!
指定初始化器的 GNU 扩展仅支持 .designator = arg
形式,不支持 C++20 中引入的 .designator{arg}
形式。在以后的 gcc 版本中,花括号初始化器形式也被接受为 gcc 扩展。
解决方法就是不使用扩展或使用等式初始值设定项:
_bus({.spi = spi, .pin = csPin}) // GNU extension or C++20
_bus{spi, csPin} // C++11
我关注class:
Foo.h
class Foo {
public:
struct Bus {
SPIClass *spi;
const uint8_t pin;
};
constexpr Foo(SPIClass *spi, uint8_t csPin) :
_bus({.spi{spi}, .pin{csPin}}) {}
protected:
const Bus _bus;
};
因为 const 成员 _bus 我在构造函数中使用初始化列表对它们进行了初始化。 但不知何故无法识别它并抛出以下编译错误:
error: no matching function for call to 'Foo::Bus::Bus(<brace-enclosed initializer list>)'
_bus({.spi{spi}, .pin{csPin}}) {}
^
note: candidate: 'constexpr Foo::Bus::Bus(const TMC::Motionctrl::Bus&)'
struct Bus {
^~~
note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const Foo::Bus&'
note: candidate: 'constexpr Foo::Bus::Bus(Foo::Bus&&)'
note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'Foo::Bus&&'
error: member 'Foo::_bus' must be initialized by mem-initializer in 'constexpr' constructor
_bus({.spi{spi}, .pin{csPin}}) {}
^
我猜第二个错误是因为初始化失败,但是我找不到错误。 我正在使用 GCC(GNU Arm 嵌入式工具链),构建命令参数是
\packages\STM32\tools\arm-none-eabi-gcc.2.1-1.7/bin/arm-none-eabi-g++" -mcpu=cortex-m3 -mthumb "@E:\xxx\Release/sketch/build_opt.h" -c -Os -Wall -Wextra -std=gnu++14 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -fno-use-cxa-atexit -MMD "-
谢谢!
指定初始化器的 GNU 扩展仅支持 .designator = arg
形式,不支持 C++20 中引入的 .designator{arg}
形式。在以后的 gcc 版本中,花括号初始化器形式也被接受为 gcc 扩展。
解决方法就是不使用扩展或使用等式初始值设定项:
_bus({.spi = spi, .pin = csPin}) // GNU extension or C++20
_bus{spi, csPin} // C++11