Assignment operator overloading


QueLinked.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef PQUEUE_H
#define PQUEUE_H
#include <stdlib.h> // Provides size_t

qstruct Node; 
    class PriorityQueue
    {
    public:
        typedef int Item;
		// constructor & destructor
        PriorityQueue( );
        PriorityQueue(const PriorityQueue& source);
        ~PriorityQueue( );

		// function that needs to be defineds
        void operator =(const PriorityQueue& source);
        void insert(const Item& entry, unsigned int importance);
        Item get_front( );
	
       size_t size() const { return many_nodes; }	// in line function
        bool is_empty( ) const { return many_nodes == 0; } // in line function

    private:
                Node* head_ptr;	// pointer to head_ptr
		Node* rear_ptr; // pointer to rear_ptr
		size_t many_nodes;
    };

    struct Node
    {   // Node for a linked list
        PriorityQueue::Item data;
        unsigned int priority;
        Node *link;
    };

#endif 


QueLinked.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void PriorityQueue::operator=(const PriorityQueue& source) //overloading the assignment operator
{
     
    //bunch of other codes


	Node* insert_ptr;
	insert_ptr = new Node;

	insert_ptr->data = source.head_ptr->data;
	insert_ptr->priority = source.head_ptr->priority;
	insert_ptr->link = source.head_ptr->link;

	rear_ptr->link = insert_ptr;
	rear_ptr = insert_ptr;
	temp_nodePtr2 = temp_nodePtr2->link;
}


Background:I'm trying to create a Queue class with priority within a linked list data structure.(Homework)

I'm currently stuck on the part where I'm trying to overload the assignment operator.

1
2
3
        insert_ptr->data = source.head_ptr->data;
	insert_ptr->priority = source.head_ptr->priority;
	insert_ptr->link = source.head_ptr->link;


this part from QueLinked.cpp gives me an exception handling every time my program gets to this part. I thought the way I currently had it would work, but I guess not. So my question? is there a way to copy the private members from the node struct from within the PriorityQueue class

First-chance exception at 0x010A783C in KingSA4.exe: 0xC0000005: Access violation reading location 0x00000008.

If there is a handler for this exception, the program may be safe

Topic archived. No new replies allowed.