【发布时间】:2016-12-29 11:15:33
【问题描述】:
我已经看到了我正在尝试使用 void function 的确切语法,但我不知道为什么它在我的代码中不起作用:
构造函数:
class Input
{
public:
Input(const std::map<std::string, void(*)(void)> &arg_0)
{ //...code...// }
};
使用临时 std::map 调用构造函数:
Input _Input(
std::map<std::string, void(*)(void)> {
{"exit", [](){exit(1);}}
}
);
还有,只是最轻微的变化
std::map<std::string, void(*)(void)> NAME = {
{"exit", [](){exit(1);}
}
足以解决问题,并且由于某种原因, NAME 也超出了范围(这就是我想要的)。所以基本上,我知道解决方案,但我想知道为什么第一个代码不起作用。
附言错误是
error: expected ‘)’ before ‘{’ token
IN
std::map<std::string, void(*)(void)> {
编辑:
我明白了,我调用构造函数的确切方式显然很重要:
class BackEnd
{
private:
Input _Input(
std::map<std::string, void(*)(void)> {
{"exit", [](){exit(1);}}
}
);
};
在这种情况下,它会停止工作并引发错误。 示例:https://ideone.com/ikGUGF
【问题讨论】:
-
@tkausl 我明白了,嗯,我要仔细检查我的代码。
-
你用的是什么编译器?
-
@BiagioFesta
sudo g++-4.8 *.cpp -o "main" -pthread -std=c++1y -Wall -pedantic-errors;但这不是问题,我已经更新了我的问题。
标签: c++ constructor c++14 initializer-list stdmap