【问题标题】:Code error when trying to print out the node of a LinkedList尝试打印出 LinkedList 的节点时出现代码错误
【发布时间】:2013-04-15 18:18:08
【问题描述】:

嘿,我基本上是想用它的元素打印出相应的节点。但我得到一个错误:Error 1 error C2440: 'return' : cannot convert from 'DoublyLinkedListNode<Datatype> *' to 'int &' 我尝试了一些事情,比如更改索引函数的返回类型,但到目前为止我没有任何乐趣。 谁能帮我解决这个问题?

这是我的代码:

//-------------------------------------------------------------------------------------------
//  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);
    }

【问题讨论】:

  • 恕我直言,显示的代码不完整,很难发现错误。
  • 错误指的是哪一行代码?我不认为这是你的问题。
  • 该错误在哪一行?我什至没有看到返回 int& 的函数
  • 错误是指打印函数中for循环中的cout行。
  • @ArgumentNullException 如果我将 index() 从 cout 中取出,则没有错误。 Index() 是唯一有问题的东西:S

标签: c++ printing indexing linked-list


【解决方案1】:

基本上我试图使用 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;


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2021-04-17
    相关资源
    最近更新 更多