Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7190

How to Initialize class member that is a pointer to a structure

$
0
0

I have a problem with the application crashing at the line of code where if(!head) is being referenced inside the function: insertNode(). head and tail are class members of type node*. It looks like, I am missing something in the way the class members: head, tail are initialized.. This is the runtime error: "Unhandled exception at 0x00245246 in SLinkedlist_array.exe: 0xC0000005: Access violation reading location 0x00000000."

    slinkedlist.h:    typedef struct node    {        int value;        struct node* next;    } node;    class slinkedlist    {    public:        //ctor, dtor, insertNode(int, int), displayList()    private:        node* head, tail;    };    slinkedlist.cpp:    bool slinkedlist::insertNode(int value, int aftNodeVal)    {        int toinsertval = value;        int searchkey = aftNodeVal;        bool retval = false;        // If it's a new linked list        if(!head)  // THIS IS WHERE THE APPLICATION CRASHES!        {            node* head = new node;            head->value = toinsertval;            head->next = NULL;            return true;        }        else //It's not a new list        {            while(head->next != NULL)            {                 //some more code here...             }        }        return retval;    }    void slinkedlist::displayList()    {        while(!head)        {            do            {                cout << head->value << "" ;                head = head->next;            }            while(head->next != NULL);        }        //return void;    }    main.cpp:    int main()    {        slinkedlist *s1 = NULL;        s1->insertNode(4, -1);        s1->displayList();        while(1);    }`

Viewing all articles
Browse latest Browse all 7190

Trending Articles