Error with T::iterator, where template parameter T might be vector<int> or list<int>

社会主义新天地 提交于 2019-11-29 02:28:35

You need typename to tell the compiler that ::iterator is supposed to be a type. The compiler doesn't know that it's a type because it doesn't know what T is until you instantiate the template. It could also refer to some static data member, for example. That's your first error.

Your second error is that v is a reference-to-const. So, instead of ::iterator you have to use ::const_iterator. You can't ask a constant container for a non-const iterator.

Change T::iterator i; to typename T::const_iterator i; because ::iterator is of type T and v is a const &.

Before a qualified dependent type, you need typename. Without typename, there is a C++ parsing rule that says that qualified dependent names should be parsed as non-types even if it leads to a syntax error.

typename states that the name that follows should be treated as a type. Otherwise, names are interpreted to refer to non-types.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!