我必须声明Iter
为typename
,但不是typedef
。
然后,我不能在我的template
class
声明中使用Iter
=>沮丧:(
请告诉我如何使用Iter
,否则解释我为什么C++是如此nasty ...模板类嵌套类型typename
template <typename T>
struct MyContainer
{
typedef std::vector<T> Vec;
typename Vec::iterator Iter;
void Fo (typename std::vector<T>::iterator); //ok
typename std::vector<T>::iterator Ba(); //ok
void Foo (Iter); //error: 'Iter' is not a type
Iter Bar(); //error: 'Iter' does not name a type
}; //(gcc 4.1.2)
- 为什么
typedef
不能用来声明Iter
?为什么不呢? - 如何内
template
class
使用Iter
?(例如,作为函数参数类型)
EDIT
在指令typename Vec::iterator Iter;
关键字typename
说Vec::iterator
是一种类型(即不是静态成员函数/属性)。因此,Iter
未声明为类型,而是作为使用Vec::iterator
类型的变量声明。
完美:)我刚才在你的答案代码源解决方案:-D感谢加;) – olibre
@olibre关于您的编辑这个答案(我回滚了):可以在枚举中通过缩进两次使用blockquote(4 + 4 = 8个空格)。不需要任何不寻常的枚举语法... – jogojapan
@jogojapan谢谢您的提示/建议:-D – olibre