0
void listord::stmp()
{
    nodeL *aux=head;
    cout<<"STMP"<<endl;
    while (aux)
    {
        cout<<aux->getData();
        aux=aux->getNext();
    }
    cout<<endl;
    return;
}

I can't understand why this simply list print code loops. I know that it could be because of a bad pointer but i don't know how to solve it.

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

This is the nodeL class, the GetNext() it's just a return next, nothing else.

  • 3
    I would say it's because `getNext()` returns a non-null pointer, but without seeing the code for that function it's tough to know what it's doing. – jkb Dec 09 '19 at 00:14
  • Do you initialize `next` anywhere in your constructors? – ChrisMM Dec 09 '19 at 00:35

1 Answers1

0

You want to set next = nullptr in your constructor. Also you want to return *& from your getNext function, like this:

nodeL *& getNext();

These two things should fix your issue.

bhristov
  • 3,137
  • 2
  • 10
  • 26