【问题标题】:How to add varible value to a brace enclose initializer list?如何将变量值添加到大括号括起来的初始值设定项列表中?
【发布时间】:2016-02-14 17:13:51
【问题描述】:

我的代码中有这个奇怪的错误,我似乎无法弄清楚发生了什么。这里知识渊博的人比我多得多,所以我想我会发布这个问题。

这段代码可以正常工作:

histogram_requests -> AddHistos("trajTree",
{
    .fill = "HitEfficiency_vs_Layers", 
    .pfs = {}, 
    .cuts = {}, 
    .draw = "LPE1", 
    .opt = "", 
    .ranges = {0.0, 0.0}
});

AddHistos 声明如下:

void AddHistos(std::string name, HistoParams hp, bool AddCutsToTitle = true);

其中 HistoParams 是一个结构体:

typedef struct HistoParams
{
    std::string fill;
    std::vector<const char*> pfs;
    std::vector<const char*> cuts;
    std::string draw;
    std::string opt;
    std::vector<double> ranges;
} HistoParams;

所以根据定义,.cuts 的类型应该是std::vector of const char*。但是每当我尝试做这样的事情时:

std::vector<const char*> added_cuts = {};
if(options.add_cuts)
{
    added_cuts.push_back("EffCuts");
}

histogram_requests -> AddHistos("trajTree",
{
    .fill = "HitEfficiency_vs_Layers",
    .pfs = {},
    .cuts = added_cuts,
    .draw = "LPE1",
    .opt = "",
    .ranges = {0.0, 0.0}
});

然后我得到以下错误:

错误:没有匹配函数调用'Custom_smart_histos::AddHistos(const char [9],大括号括起来的初始化列表)'

谁能告诉我为什么会出现此错误,或者如何正确执行此操作? 非常感谢!

【问题讨论】:

  • 您知道您正在使用编译器扩展吗?这从一开始就不是有效的 C++。
  • 这在c++11中有效。
  • 指定的初始化器仍然不在 C++14 中。但自 VisualStudio 2013 以来,它们一直是 Microsoft C++ 的一项功能。

标签: c++ initializer-list curly-braces


【解决方案1】:

我知道问题出在哪里,抱歉问题含糊不清。 我所知道的 Histoparams 的实现是一个古老的实现。由于我包含的标题被保护了,我什至没有注意到它被覆盖了。而新的定义削减为std::vector。切换到那个解决了问题。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 2021-11-21
    • 2014-05-13
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多