char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords;
ReservedWords.insert(std::begin(types),std::end(types));
这里的前两行是声明,因为它们声明了变量(types
和ReservedWords
)。第三行不是声明,它只是一个表达式语句,所以它出现在函数之外是不合法的。
您可以执行以下操作:
char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> MakeReservedWords() {
std::set<std::string> tmp;
tmp.insert(std::begin(types), std::end(types));
return tmp;
}
std::set<std::string> ReservedWords(MakeReservedWords());
鉴于您使用的是 C++11,您应该能够做到这一点:
std::set<std::string> ReservedWords { "char", "short", "int", "long", "float", "double", "void"};
如果您的编译器不支持 C++11 的这一部分,您将不得不接受这样的事情(正如@juanchopanza 所建议的那样):
char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords(std::begin(types), std::end(types));