I have a cpp file that contains the following:
char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords;
ReservedWords.insert(std::begin(types),std::end(types));
this gives an error missing type specifier - int assumed. Note: C++ does not support default-int
I have read that you can’t write statements in a global scope, is this the case here ?
I don’t completely understand the rule, and would like to know where its best to put this code ? (header file, inside a function etc…)
The first two lines here are declarations because they declare variables (
typesandReservedWords). The third line is not a declaration, it’s just an expression statement so it’s not legal for it to appear outside a function.You could do something like:
Given that you are using C++11 you should be able to do this:
If your compiler doesn’t support this part of C++11 you will have to settle for something like this (as suggested by @juanchopanza):