试图打印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作为索引,我被告知不是这样做的方式,并在打印方法的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;
}
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误