title: C++ Forward Declaration of Objects
body: <p>i'm trying to figure out how to forward declare objects where new objects are storing other new objects for example:</p>
  
  <p>A Queue, holding Nodes, these Nodes hold Data items which hold 2 Lists or preferably 2 Queues.</p>
  
  <p>Here is my Data.h</p>
  
  <pre><code>#ifndef DATA_H_
  #define DATA_H_
  
  class List;
  class Data {
  public:
      Data();
      Data(int aID);
      virtual ~Data();
      virtual int getID();
      virtual void setID(int aID);
  private:
      int ID;
      List *listOne;
      List *listTwo;
  };
  
  #endif /* DATA_H_ */
  </code></pre>
  
  <p>And my Data.c</p>
  
  <pre><code>#include "Data.h"
  #include "List.h"
  
  Data::Data() {
      listOne = new List();
      listTwo = new List();
      Data::ID = 0;
  }
  
  Data::Data(int aID) {
      Data::listOne = new List();
      Data::listTwo = new List();
      Data::ID = aID;
  }
  
  Data::~Data() {
      delete(listOne);
      delete(listTwo);
  }
  
  int Data::getID() {
      return Data::ID;
  }
  
  void Data::setID(int aID) {
      Data::ID = ID;
  }
  </code></pre>
  
  <p>The error message:</p>
  
  <pre><code>..\Data.cpp: In constructor 'Data::Data()':
  ..\Data.cpp:12:23: error: invalid use of incomplete type 'class List'
    listOne = new List();
                         ^
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp:13:22: error: invalid use of incomplete type 'class List'
    listTwo = new List();
                        ^
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp: In constructor 'Data::Data(int)':
  ..\Data.cpp:18:29: error: invalid use of incomplete type 'class List'
    Data::listOne = new List();
                               ^
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp:19:28: error: invalid use of incomplete type 'class List'
    Data::listTwo = new List();
                              ^
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp: In destructor 'virtual Data::~Data()':
  ..\Data.cpp:24:18: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
    delete(listOne);
                    ^
  ..\Data.cpp:24:18: warning: invalid use of incomplete type 'class List'
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp:24:18: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
    delete(listOne);
                    ^
  ..\Data.cpp:25:17: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
    delete(listTwo);
                   ^
  ..\Data.cpp:25:17: warning: invalid use of incomplete type 'class List'
  In file included from ..\Data.cpp:8:0:
  ..\Data.h:11:7: note: forward declaration of 'class List'
   class List;
         ^
  ..\Data.cpp:25:17: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
    delete(listTwo);
  </code></pre>
  
  <p>Now if I remove the forward delclaration in Data.h and have Data.h include List.h I will get: and result in a circular inheritance cycle</p>
  
  <pre><code>..\Data.h:25:2: error: 'List' does not name a type
    List *cpubursts;
    ^
  ..\Data.h:26:2: error: 'List' does not name a type
    List *iobursts;
  </code></pre>
  
  <p>How will I go about fixing this sort of setup?</p>
  
  <p>Edit: List.h</p>
  
  <pre><code>#ifndef LIST_H_
  #define LIST_H_
  
  #include "Node.h"
  
  namespace std {
  
  class List {
  private:
      Node *top;
  public:
      List();
      virtual ~List();
      virtual Node* getTop();
      virtual void add(Node * linkedNode);
      virtual void remove(Node * linkedNode);
  };
  
  } /* namespace std */
  
  #endif /* LIST_H_ */
  </code></pre>
  
  <p>Node.h</p>
  
  <pre><code>#ifndef NODE_H_
  #define NODE_H_
  
  #include "Data.h"
  
  namespace std {
  
  class Node {
  public:
      Node();
      Node(Data * aItem);
      virtual ~Node();
      virtual Node* getNext();
      virtual void setNext(Node * linkedNode);
      virtual Data* getData();
      virtual void setData(Data* item);
  private:
      Node *next;
      Data *item;
  };
  
  } /* namespace std */
  
  #endif /* NODE_H_ */
  </code></pre>