Need help with c++ pointers

Hello, I just got started with c++ and was trying to create a Node class to use for linked lists. I tried to use a pointer in this class that is set to the next Node. It didnt work and I dont know why, it seems to set the wrong pointer to the third Node i created in the main function. If anyone could explain why it does not work it would be great.

class Node {
    public:
        Node *pNext;
        double val;
        Node(double num) {val = num;}
        Node getNext() {return *pNext;}
        void setNext(Node node) {pNext = &node;}
        void show() {cout<<val<<" ";}
};

int main() {
    Node b(8), c(7), d(5);
    b.setNext(c);
    c.setNext(d);
    b.show();
    b.getNext().show();
    b.getNext().getNext().show();
    cout<<endl;
    // prints 8 5 -2.53017e-98 
    // but should print 8 7 5
    return 0;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.