質問

I'm not sure if I fully understand the problem I'm having, but as far as I can work out, it's something along the following lines.

I'm trying to make a class to store a list of doubly linked nodes which can store data. I'm also trying to make an iterator for this class.

The list class and iterator class are separate but obviously depend on one another, however it seems that because of their interdependency, one or the other will not function properly.

I've consolidated both classes into a single file for now and will include the code for reference below:

#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

Is there a way around this problem or have I entirely misidentified it entirely? What led to my conclusion was that when I switch the order of the classes (i.e. place the DListIterator class above the DLinkedList class), I get a different selection of errors/warnings (I will include these warnings below for reference)

If anyone would like to see more of the code for reference, just let me know.

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
役に立ちましたか?

解決

Use a forward declaration for the second class and move member functions out of the first class definition as neccessary. Using inline and keeping the definitions in the header preserves intended inline semantics.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top