0

I am trying to create a Doubly Linked List and respective node class and am having an issue trying to add the data types of head and tail to my IntDLList class. I'm not quite sure what I missed, but an error is occurring stating that both head and tail were not declared, and that my Node class does not take a type. Any help is appreciated!

Edit: This does not seem to be a duplicate question, I looked at the other answers and trying to resolve an invalid use of incomplete type did not solve the same issue as my name type error.

IntDLList

using namespace std;

template <class T>
class IntDLList {
public:
       IntDLList() {
           head=tail=0; // error: 'head' was not declared in this scope (& same for tail)
       }
       ~IntDLList();
       int isEmpty() {
           return head==0; // error: 'head' was not declared in this scope
       }
       void addToDLLHead(const T&);
       void addToDLLTail(const T&);
       T deleteFromDLLHead();
       T deleteFromDLLTail();
       void deleteDLLNode(const T&);
       bool isInList(const T&) const;
       void showList();
private:
       IntDLLNode<T> *head, *tail; //error: IntDLLNode does not name a type
};

IntDLLNode

using namespace std;

template<class T>
class IntDLLNode {
    friend class IntDLList;
    public:
        IntDLLNode() {next = prev = 0;}
        IntDLLNode(const T& el, IntDLLNode *n = 0, IntDLLNode *p = 0) {
            info = el;
            next = n;
            prev = p;

        }
    protected:
         T info; 
         IntDLLNode<T> *next,*prev;
    private:

};
John Harding II
  • 564
  • 1
  • 6
  • 20
  • 4
    Possible duplicate of [Resolving a Circular Dependency between Template Classes](https://stackoverflow.com/questions/3353831/resolving-a-circular-dependency-between-template-classes) – Borgleader Dec 08 '18 at 20:42
  • Looked at the possible duplicate and I believe that my classes don't demonstrate any circular dependency – John Harding II Dec 08 '18 at 20:47
  • 1
    The error `IntDLLNode does not name a type` means that either you forgot to `#include` the header where `IntDLLNode` is defined, or both headers include each other either directly or indirectly, which is a circular dependency. – Kevin Dec 08 '18 at 20:49
  • 2
    @JohnHardingII Your classes obviously depend on each other (`IntDLList` has members of type `IntDLLNode *` and `IntDLLNode` wants to be friends with `IntDLList`), so you have a circular dependency chain. Adding a forward declaration as suggested in an answer to the other question makes things compile. – melpomene Dec 08 '18 at 20:49
  • 1
    Live demo: https://ideone.com/v07ReY – melpomene Dec 08 '18 at 20:51
  • That makes sense, I can see the dependency. @melpomene Thank you for the live demo. Added the forward, and the only issue I have now is the compiler is saying that "IntDLList is not a class template" where it is declared as a friend class in IntDLLNode – John Harding II Dec 08 '18 at 21:01
  • 1
    @JohnHardingII Did you see the change I made to that line? In my code it's `friend class IntDLList;` (note the ``). If that's not the issue, is the declaration of `IntDLList` visible where you declare `IntDLLNode` (header is included, etc)? – melpomene Dec 08 '18 at 21:04
  • @melpomene, yes that was added- just a moment – John Harding II Dec 08 '18 at 21:05
  • @melpomene For some reason including the list class in my node header was not enough, it needed to be directly included in the class- but we're up in functioning now. Thank you for the help. I'll accept as answer if you add it – John Harding II Dec 08 '18 at 21:15

0 Answers0