简体   繁体   中英

Typedef-type inside template class gives compile error

Real code, stripped to simplest relevant parts:

3rd-party code

template<class T>
class Ptr
{
private:

    typedef Ptr this_type;

public:

    typedef T element_type;
};

typedef Ptr<DataSet> DataSetPtr;

My Code - GPtr.h

template<class BaseType>
class GPtr
{
    ///force BaseType to be a Ptr or subclass
    typedef BaseType::element_type WrappedType;
public:
    ...
};

If I remove the reference to BaseType::element_type it compiles fine, but with it in I get compile warnings/errors:

  • warning C4346: 'BaseType::element_type' : dependent name is not a type prefix with 'typename' to indicate a type
  • error C2146: syntax error : missing ';' before identifier 'WrappedType'

I am not even using GPtr anywhere in my code as a specific templated type... simply including gptr.h leads to the problem.

What is wrong here?

You have to use the typename disambiguator:

typedef typename BaseType::element_type WrappedType;
//      ^^^^^^^^^

Without it, the compiler won't parse element_type as the name of a type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM