开发者

C++ template + typedef

What is wrong in the following code:

Point2D.h

template <class T> 
class Point2D 
{     
   private:
         T x;
         T y; 
   ... 
 };

PointsL开发者_开发技巧ist.h

template <class T>
class Point2D;

template <class T>
struct TPointsList
{
    typedef std::vector <Point2D <T> > Type;
};

template <class T>
class PointsList
{
    private:
            TPointsList <T>::Type points;  //Compiler error
 ...
};

I would like to create new user type TPointsList without direct type specification...


Add typename:

...
typename TPointsList<T>::Type points;
...

See Why do we need typename here?


have you tried using the typename keyword?

template <class T>
class Points2DList
{
    private:
            typename TPoints2DList <T>::Type points;  //using the typename keyword
 ...
};


Others have already answered your question, but I think, if you want to know why typename is required there, then you may see this topic:

Use of typename keyword with typedef and new


The question is a bit unclear, but it appears that you are trying to instantiate a vector of Point2D without having the definition of the Point2D template available. Try adding #include "Point2D.h" to the top of PointsList.h. As other answerers have mentioned, you are also attempting to use a qualified dependant type without a typename, so you should also add change the line

TPointsList <T>::Type points;  //Compiler error

to:

typename TPointsList <T>::Type points;


What is TPoints2DList? It's not declared anywhere.

Now that TPoints2DList is declared as a struct, it has be referenced as such:

   private:
            struct TPointsList <T>::Type points;  //should compile now
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜