【发布时间】: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