“expected type-specifier” error in g++

2019-04-02 19:09发布

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()?

标签: c++ g++
3条回答
Lonely孤独者°
2楼-- · 2019-04-02 19:52
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楼-- · 2019-04-02 20:04

Make that

new typename Container::iterator(

For a thorough explanation, see this FAQ.

查看更多
成全新的幸福
4楼-- · 2019-04-02 20:10

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.

查看更多
登录 后发表回答