缺少类型说明符 - 假设为 int。注意:C++ 不支持 default-int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10967375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
missing type specifier - int assumed. Note: C++ does not support default-int
提问by Michael
I have a cpp file that contains the following:
我有一个包含以下内容的 cpp 文件:
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
这给出了一个错误 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...)
我不完全理解规则,想知道把这段代码放在哪里最好?(头文件,在函数内等......)
回答by Luchian Grigore
First, not that std::begin
and std::end
are C++11, so are you sure you have a compatible compiler and that you're compiling with C++11 support?
首先,不是那个,std::begin
而且std::end
是 C++11,所以你确定你有一个兼容的编译器并且你是用 C++11 支持编译的吗?
I don't believe this is theerror though. Are you including:
我不相信这是在虽然错误。你包括:
#include <string>
#include <set>
#include <iterator>
?
?
回答by CB Bailey
char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords;
ReservedWords.insert(std::begin(types),std::end(types));
The first two lines here are declarations because they declare variables (types
and ReservedWords
). 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.
这里的前两行是声明,因为它们声明了变量(types
和ReservedWords
)。第三行不是声明,它只是一个表达式语句,所以它出现在函数之外是不合法的。
You could do something like:
你可以这样做:
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());
Given that you are using C++11 you should be able to do this:
鉴于您使用的是 C++11,您应该能够做到这一点:
std::set<std::string> ReservedWords { "char", "short", "int", "long", "float", "double", "void"};
If your compiler doesn't support this part of C++11 you will have to settle for something like this (as suggested by @juanchopanza):
如果您的编译器不支持 C++11 的这一部分,您将不得不接受这样的事情(如@juanchopanza 所建议的那样):
char const* types[] = { "char", "short", "int", "long", "float", "double", "void"};
std::set<std::string> ReservedWords(std::begin(types), std::end(types));
回答by justcoder
This error appears when you don't include the correct files. Make sure to add #include <string.h>
当您不包含正确的文件时,会出现此错误。确保添加#include <string.h>
And yes, you must remove this line from global scope:
是的,您必须从全局范围中删除此行:
ReservedWords.insert(std::begin(types),std::end(types));
Try putting it in the main function.
试着把它放在主函数中。