gpt4 book ai didi

c++ - 将 std::array 传递给函数:默认值

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:58 32 4
gpt4 key购买 nike

我有一个函数签名如下:

void analyze(Image * x, std::array<bool, 4> smooth);

如果用户没有在调用中明确设置它,我想给这个数组 smooth 一个默认值。所以我尝试了类似的方法:

std::array<bool, 4> smooth = std::array<bool, 4>
({true, true, true, false}));
void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>(({true, true, true, false})));

无论我尝试什么,我都无法编译它。上面的例子说“错误:预期的表达”。

最佳答案

这个有效:

void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>({true, true, true, false}));

这有效:

void analyze(Image * x, std::array<bool, 4> smooth = std::array<bool, 4>{true, true, true, false});

用初始化列表初始化数组。您的代码有额外的括号。

关于c++ - 将 std::array 传递给函数:默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930158/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com