c++ - 为什么这个函数会循环?
问题描述
void listord::stmp()
{
nodeL *aux=head;
cout<<"STMP"<<endl;
while (aux)
{
cout<<aux->getData();
aux=aux->getNext();
}
cout<<endl;
return;
}
我不明白为什么这只是列出打印代码循环。我知道这可能是因为指针不好,但我不知道如何解决它。
class nodeL{
private:
int data;
nodeL *next;
public:
nodeL();
nodeL(int d);
nodeL(int d,nodeL *n);
int getData();
nodoL *getNext();
void setData(int d);
void setNext(nodeL *n);
};
这是 nodeL 类,GetNext()
它只是下一个返回,没有别的。
解决方案
你想next = nullptr
在你的构造函数中设置。您还想*&
从 getNext 函数返回,如下所示:
nodeL *& getNext();
这两件事应该可以解决您的问题。
推荐阅读
- reactjs - 输入“鼠标事件”
' 不可分配给类型 'MouseEvent ' - crystal-lang - 斐波那契问题导致算术溢出
- java - 非递归二分查找
- python - AWS Python Lambda 函数 JSON 格式
- javascript - 创建多个按钮 - Javascript
- python - sqlalchemy 上的 DataFrame 打印数字而不是实际的列名
- c# - 在 .NET 核心应用程序中使用 WinRT 组件
- python - 为什么我在此程序中收到“列表索引超出范围”错误?
- python - 使用 set 和 line.split python
- android - Android有没有办法设置所有小部件的样式?