“expected type-specifier” error in g++

倾然丶 夕夏残阳落幕 提交于 2020-01-01 07:24:06

问题


I have a DD class

template<typename T>
class DD
: public IEnumerable<T>
{
    typedef IEnumerable<T> Super;
    typedef std::set<T*> Container;

And a method

template<typename T>
bool DD<T>::Enumerator::Move()
{
    if(!mIt.get()) 
       mIt.reset(
          new Container::iterator( <-----
            mContainer.GetContainer().begin()
          )
       );
       ... 
}

When I compile the class, I got error: expected type-specifier. What's wrong with the Container::iterator()?


回答1:


Try:

new typename Container::iterator

When you are in a C++ template, the compiler doesn't know whether Container::iterator is a type or something else. So you need to explicitly say that its a type.

On another note, creating an iterator with new is almost certainly wrong.




回答2:


new typename Container::iterator( 
//  ^^^^^^^^

Without the typename, C++ will assume X::Y is a member (value/function) when X is in a template. You need the typename to force the compiler to interpret X::Y as a type.




回答3:


Make that

new typename Container::iterator(

For a thorough explanation, see this FAQ.



来源:https://stackoverflow.com/questions/5250526/expected-type-specifier-error-in-g

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