嵌套模板类的前向声明

时间:2013-11-12 17:20:08

标签: c++ templates

我有一个类似的课程:

template< typename T, typename Allocator >
class MemMngList : protected LockFreeMemMng<Node, Link>
{
public:
    typedef T Type;
    typedef Allocator AllocatorType;

    struct Node : public LockFreeNode
    {
    public:
        struct Link : protected LockFreeLink< Node >
        {
                    ....

问题是我在LockFreeMemMng的模板参数中遇到错误('Node':未声明的标识符......)。

如何在MemMngList实现的上方使用NodeLink的前向声明?

template< typename T, typename Allocator >
class MemMngList;

//Forward Declaration of Node and Link

1 个答案:

答案 0 :(得分:2)

您无法在类声明中转发声明内容。如果你想访问私人成员,你需要将它移到课堂外的某个地方并使用朋友:

template <typename T, typename Allocator>
struct NodeType : public LockFreeNode< NodeType<T,Allocator> >
{
    ...

    template <typename,typename>
    friend class MemMngList;
};

template <typename T, typename Allocator>
struct LinkType : public LockFreeLink< NodeType <T,Allocator> >
{
    ...

    template <typename,typename>
    friend class MemMngList;
};

template< typename T, typename Allocator >
class MemMngList : protected LockFreeMemMng< NodeType <T,Allocator> , LinkType <T,Allocator> >
{
    typedef NodeType <T,Allocator> Node;
    typedef LinkType <T,Allocator> Link;

    ...
};