C++ 尝试打印LinkedList的节点时出现代码错误
嘿,我基本上是想打印出相应的节点及其元素。但我收到一个错误:C++ 尝试打印LinkedList的节点时出现代码错误,c++,printing,indexing,linked-list,C++,Printing,Indexing,Linked List,嘿,我基本上是想打印出相应的节点及其元素。但我收到一个错误:error 1 error C2440:“return”:无法从“DoublyLinkedListNode*”转换为“int&” 我尝试过一些事情,比如改变索引函数的返回类型,但到目前为止,我没有任何乐趣。 有人能帮我修一下吗 这是我的密码: //------------------------------------------------------------------------------------------- //
error 1 error C2440:“return”:无法从“DoublyLinkedListNode*”转换为“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);
}
//-------------------------------------------------------------------------------------------
//姓名:打印。
//描述:打印列表中的元素及其索引。
//-------------------------------------------------------------------------------------------
作废打印(双链接列表和p_列表)
{
//设置一个新的迭代器。
DoublyLinkedListIterator itr=getIterator();
for(itr.Start();itr.Valid();itr.Forth())
{
cout基本上,我试图使用m_节点作为索引,但我被告知这不是一种方法,并在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;
}
void打印(双链接列表和p_列表)
{
int指数=0;
//设置一个新的迭代器。
DoublyLinkedListIterator itr=getIterator();
for(itr.Start();itr.Valid();itr.Forth())
{
索引++;
cout IMHO,显示的代码不完整,很难发现错误。错误指的是哪一行代码?我不认为这在你的问题中。错误在哪一行?我甚至没有看到函数返回int&错误指的是打印函数中for循环中的cout行。@ArgumentNullException如果我使用index()没有错误。Index()是唯一有问题的东西:S