0

내가 가진 문제를 완전히 이해하고 있는지는 잘 모르겠지만, 해결할 수있는 한 다음 행에있는 내용입니다.상호 의존적 인 클래스에서 발생할 수있는 문제

데이터를 저장할 수있는 이중 연결된 노드 목록을 저장하는 클래스를 만들려고합니다. 나는 또한이 클래스의 반복자를 만들기 위해 노력하고있다.

목록 클래스와 반복자 클래스는 별개이지만 분명히 서로 의존하지만, 상호 의존성 때문에 서로가 제대로 작동하지 않는 것으로 보입니다.

내가 지금은 하나의 파일에 두 클래스를 모두 통합 한 아래 참조를 위해 코드를 포함합니다 :

#ifndef DLINKEDLIST_H 
#define DLINKEDLIST_H 

#include "Header.h" 
#include "DListNode.h" 
#include "DListIterator.h" 

    class DLinkedList 
{ 
public: 
    DListNode* m_head; 
    DListNode* m_tail; 
    int m_count; 

    DLinkedList() 
    { 
     m_head = 0; 
     m_tail = 0; 
     m_count = 0; 
    } 

    ~DLinkedList() 
    { 
     // temporary node pointers. 
     DListNode* itr = m_head; 
     DListNode* next; 
     while (itr != 0) 
     { 
      // save the pointer to the next node. 
      next = itr->m_next; 
      // delete the current node. 
      delete itr; 
      // make the next node the current node. 
      itr = next; 
     } 
    } 

    void Append(Player p_data) 
    { 
     if (m_head == 0) 
     { 
      // create a new head node. 
      m_head = m_tail = new DListNode; 
      m_head->m_data = p_data; 
     } 
     else 
     { 
      // insert a new node after the tail and reset the tail. 
      m_tail->InsertAfter(p_data); 
      m_tail = m_tail->m_next; 
     } 
     m_count++; 
    } 

    void Prepend(Player p_data) 
    { 
     // create the new node. 
     DListNode* newnode = new DListNode; 
     newnode->m_data = p_data; 
     newnode->m_next = m_head; 
     // set the head node and the tail node if needed. 
     m_head = newnode; 
     if (m_tail == 0) 
      m_tail = m_head; 
     m_count++; 
    } 

    void RemoveHead() 
    { 
     DListNode* node = 0; 

     if (m_head != 0) 
     { 
      // make node point to the next node. 
      node = m_head->m_next; 
      // then delete the head and make the pointer point to node. 

      delete m_head; 
      m_head = node; 

      // if the head is null, then you’ve just deleted the only node 
      // in the list. set the tail to 0. 
      if (m_head == 0) 
       m_tail = 0; 
      m_count--; 
     } 
    } 

    void RemoveTail() 
    { 
     DListNode* node = m_head; 
     // if the list isn’t empty, then remove a node. 
     if (m_head != 0) 
     { 
      // if the head is equal to the tail, then 
      // the list has 1 node, and you are removing it. 
      if (m_head == m_tail) 
      { 
       // delete the node and set both pointers 
       // to 0. 
       delete m_head; 
       m_head = m_tail = 0; 
      } 
      else 
      { 
       // skip ahead until you find the node 
       // right before the tail node 
       while (node->m_next != m_tail) 
        node = node->m_next; 
       // make the tail point to the node before the 
       // current tail and delete the old tail. 
       m_tail = node; 
       delete node->m_next; 
       node->m_next = 0; 
      } 
      m_count--; 
     } 
    } 

    DListIterator GetIterator() 
    { 
     return DListIterator(this, m_head); 
    } 
}; 

class DListIterator 
{ 
public: 
    DListNode* m_node; 
    DLinkedList* m_list; 

    DListIterator(DLinkedList* p_list = 0, DListNode* p_node = 0) 
    { 
     m_list = p_list; 
     m_node = p_node; 
    } 

    void Start() 
    { 
     if (m_list != 0) 
      m_node = m_list->m_head; 
    } 

    void Forth() 
    { 
     if (m_node != 0) 
      m_node = m_node->m_next; 
    } 

    Player Item() 
    { 
     return m_node->m_data; 
    } 

    bool Valid() 
    { 
     return (m_node != 0); 
    } 
}; 

#endif 

이 문제를 해결 방법이 있나요 아니면 완전히 완전히를 잘못 식별 한? 결론적으로 클래스의 순서를 바꿀 때 (즉, DLinkedList 클래스 위에 DListIterator 클래스를 배치하면) 다른 오류/경고를 얻을 수 있습니다 (참조를 위해 아래의 경고가 포함됩니다)

누구든지 참조 할 수 있도록 더 많은 코드를보고 싶다면 알려주세요.

WARNINGS/ERRORS 
Error 1 error C2146: syntax error : missing ';' before identifier 'GetIterator' 
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Warning 4 warning C4183: 'GetIterator': missing return type; assumed to be a member function returning 'int' 
Error 5 error C3861: 'DListIterator': identifier not found 
Error 6 error C2440: 'initializing' : cannot convert from 'int' to 'DListIterator 
+1

가능한 [복잡한 순환 종속성] (http://stackoverflow.com/questions/5363542/complex-circular-dependency)의 중복 –

답변

0

두 번째 클래스에는 전달 선언을 사용하고 첫 번째 클래스 정의에서 멤버 함수를 필요한 것으로 이동합니다. inline을 사용하고 헤더에 정의를 유지하면 의도 한 인라인 의미가 보존됩니다.