C++ 嵌套名称说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6489351/
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
nested-name-specifier
提问by akashihi
I have a code like:
我有一个代码,如:
namespace mymap {
template <class Key,template <typename T > class Allocator> myownmap {
typedef pair<const unsigned int, Key> typename _myPair;
typedef multimap<unsigned int, Key,less<Key> ,Allocator<_myPair> > typename _entriesType;
}
}
It compiles successfully (and works) under MSVC, but gcc is complaining about invalid syntax:
它在 MSVC 下成功编译(并运行),但 gcc 抱怨语法无效:
.hpp:20: error: expected nested-name-specifier before ‘_myPair'
.hpp:20: error: two or more data types in declaration of ‘_myPair'
what i'm doing wrong?
我做错了什么?
回答by Bo Persson
The typename
is not needed there, and is therefore not allowed.
将typename
不需要在那里,因此是不允许的。
MSVC do not parse templates properly until they are actually used, so some errors are not found until later.
MSVC 在实际使用之前不会正确解析模板,因此有些错误直到后来才发现。
回答by Grigor Gevorgyan
"expected nested-name-specifier" means that after typename
keyword you are expected to use some nested name of a template parameter, for example typedef typename Key::iterator ...
. In your case you don't have to use typename
.
“预期的嵌套名称说明符”意味着在typename
关键字之后,您应该使用模板参数的某个嵌套名称,例如typedef typename Key::iterator ...
. 在您的情况下,您不必使用typename
.
回答by iammilind
typedef pair<const unsigned int, Key> /*typename*/ _myPair;
^^^^^^^^^^^^ not needed
See the gcc-4.5 output here. (it holds true for myownmap
being class
or function)
在此处查看gcc-4.5 输出。(它适用于myownmap
存在class
或功能)