CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2004
    Posts
    12

    Unhappy Circular template dependency

    Hi

    Can anyone help me work out a way around the following problem:

    typedef SmartPtr<LinkedListObject<MyPointer> > MyPointer;

    Unsurprisingly (I guess) gives me:

    `MyPointer' was not declared in this scope
    template argument 1 is invalid
    ISO C++ forbids declaration of `type name' with no type


    SmartPtr<T> is a smart pointer class that wraps type 'T' providing wrapped
    operations of T* - dereference, equality, implicit conversion, and arithmetic.

    Unfortunately the LinkedListObject _needs_ to store its next and prev pointers
    as pointers of type MyPointer. This whole setup arises from defining different types of LinkedListObject that are passed as a template argument to a class that creates a singly linked list or a class that creates a doubly linked list and these classes must themselves keep internal pointers using the smart pointer 'MyPointer'.

    Any help would be greatly appreciated!

    Many thanks!

  2. #2
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    Re: Circular template dependency

    I dont see any circular dependency issues here. It would be a circular dependency issue only if the smartptr class needed to know the definition of your LinkedListObject class, which is not the case here.

    All you need is:

    in your linked list class header

    #include "your smartptr header"

    // use the smart ptr

    in your main or SOME OTHER .cpp /.h file that needs to use the linked list:

    #include "your smartptr header"
    #include "your linked list class header"

    typedef SmartPtr<LinkedListObject<MyPointer> > MyPointer;

    hope this helps

  3. #3
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Circular template dependency

    Quote Originally Posted by raghuvamshi
    I dont see any circular dependency issues here.
    Yes there is. Check out the MyPointer typedef. The SmartPtr template has a parameter which in turn has its own template parameter which is the SmartPtr type. Looks like circular dependecy to me.

    Basically there is no way you can solve this. A template type cannot have itself as a template parameter. That's kinda like you being your own father.

    I would say your design is a bit off if you need such a construct. Is there really a need for the pointer type to be a template parameter of the LinkedListObject class? How about something like this instead?
    Code:
    #include <memory>
    
    template<typename T, template<typename> class P = std::auto_ptr>
    class linked_list
        {
        private:
            class node;
    
            typedef node node_type;
            typedef P<node_type> pointer_type;
    
            class node
                {
                public:
                    T value;
                    pointer_type m_next;
                };
    
            pointer_type start;
    
        public:
            linked_list()
                :
                start(new node_type)
    
                {}
        };
    
    int main()
        {
        linked_list<int> a_list;
        }
    Insert entertaining phrase here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured