繁体   English   中英

尝试打印出LinkedList的节点时出现代码错误

[英]Code error when trying to print out the node of a LinkedList

嘿,我基本上是尝试打印出带有其元素的相应节点。 但是我得到了一个错误: Error 1 error C2440: 'return' : cannot convert from 'DoublyLinkedListNode<Datatype> *' to 'int &'我尝试了一些类似更改Index函数返回类型的操作,但是到目前为止,我仍然很不高兴。 谁能帮我解决这个问题?

这是我的代码:

//-------------------------------------------------------------------------------------------
//  Name:           Print.
//  Description:    Prints the elements from the list along with its index.
//-------------------------------------------------------------------------------------------
    void Print(DoublyLinkedList<int>& p_list)
    {
    //Set up a new Iterator.
    DoublyLinkedListIterator<int> itr = getIterator();
    for(itr.Start(); itr.Valid(); itr.Forth())
        {
        cout <<"Index: "<<itr.Index() << "Element: " << itr.Item() << "\n";
        }
    cout << endl;


    }
//-------------------------------------------------------------------------------------------
//  Class:  DoublyLinkedIterator.
//-------------------------------------------------------------------------------------------
template <class Datatype>
class DoublyLinkedListIterator
{
public:
//-------------------------------------------------------------------------------------------
//  Member Vairables.
//-------------------------------------------------------------------------------------------
    DoublyLinkedListNode<Datatype>* m_node;
    DoublyLinkedList<Datatype>* m_list;
    DoublyLinkedListIterator(DoublyLinkedList<Datatype>* p_list= 0, DoublyLinkedListNode<Datatype>* p_node= 0)
    {
        m_list= p_list;
        m_node= p_node;
    }

// ------------------------------------------------------------------
//  Name:           Start
//  Description:    Resets the iterator to the beginning of the list.
//  Arguments:      None.
//  Return Value:   None.
// ------------------------------------------------------------------
    void Start()
    {
        if(m_list!= 0)
            m_node= m_list -> m_head;
    }

// ----------------------------------------------------------------
//  Name:           End
//  Description:    Resets the iterator to the end of the list
//  Arguments:      None.
//  Return Value:   None.
// ----------------------------------------------------------------
    void End()
    {
        if(m_list!= 0)
            m_node = m_list->m_tail;
    }

// ----------------------------------------------------------------
//  Name:           Forth
//  Description:    Moves the iterator forward by one position
//  Arguments:      None.
//  Return Value:   None.
// ----------------------------------------------------------------
    void Forth()
    {
        if(m_node!= 0)
            m_node= m_node->m_next;
    }

// ----------------------------------------------------------------
//  Name:           Back
//  Description:    Moves the iterator backward by one position.
//  Arguments:      None.
//  Return Value:   None.
// ----------------------------------------------------------------
    void Back()
    {
        if(m_node!= 0)
            m_node = m_node->m_prev;
    }


// ----------------------------------------------------------------
//  Name:           Item
//  Description:    Gets the item that the iterator is pointing to.
//  Arguments:      None.
//  Return Value:   Reference to the data in the node.
// ----------------------------------------------------------------
    Datatype& Item()
    {
        return m_node->m_data;
    }


// ----------------------------------------------------------------
    //  Name:           Index
    //  Description:    Gets the index that the iterator is pointing to.
    //  Arguments:      None.
    //  Return Value:   Reference to the data in the node.
    // ----------------------------------------------------------------
        Datatype& Index()
        {
            return m_node;
        }
// ----------------------------------------------------------------
//  Name:           Valid
//  Description:    Determines if the node is valid.
//  Arguments:      None.
//  Return Value:   true if valid
// ----------------------------------------------------------------
    bool Valid()
    {
        return (m_node!= 0);
    }

基本上,我试图将m_node用作索引,但有人告诉我该方法不可行,并且在print方法的for循环期间增加了变量。 这可以正常工作,下面是代码:

void Print(DoublyLinkedList<int>& p_list)
    {
        int index = 0;
    //Set up a new Iterator.
    DoublyLinkedListIterator<int> itr = getIterator();
    for(itr.Start(); itr.Valid(); itr.Forth())
        {
        index++;
        cout <<"Index: "<< index << "Element: " << itr.Item() << "\n";
        }
    cout <<"Number of Elements in the List: " << m_count << endl;


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM