Missing Element in Linked List

Hi !
As a school task I need to code a map template to make this main function work :
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
#include "Map.h"														// Defines template Map<Key, Value>
#include "Employee.h"													// Defines class Employee
#include <iostream>

using namespace std;

int main() 
{
	typedef unsigned int ID; 											// Identification number of Employee
	Map<ID, Employee> database;											// Database of employees

	database.add(761028073, Employee("Jan Kowalski", "salesman", 28)); 	// Add first employee: name: Jan Kowalski, position: salseman, age: 28,
	database.add(510212881, Employee("Adam Nowak", "storekeeper", 54));	// Add second employee: name: Adam Nowak, position: storekeeper, age: 54
	database.add(730505129, Employee("Anna Zaradna", "secretary", 32));	// Add third employee: name: Anna Zaradna, position: secretary, age: 32

	cout << database << endl;											// Print database
	
	Map<ID, Employee> newDatabase = database;							// Make a copy of database (copy constructor called)
	
	Employee* pE;
	pE = newDatabase.find(510212881);									// Find employee using its ID
	pE->position = "salesman";											// Modify the position of employee
	pE = newDatabase.find(761028073);									// Find employee using its ID
	pE->age = 29;														// Modify the age of employee

	database = newDatabase;												// Update original database (assignment operator called)
	
	cout << database << endl;											// Print original database

}


Here is my Employee.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
37
38
39
40
41
42
43
44
45
46
#pragma once
#include <string>
using namespace std;

class Employee
{
public:
	string name;
	string position;
	int age;
	Employee(string n,string p,int a);
	Employee(const Employee& E);
	Employee();
	Employee & operator=(const Employee& E);
};

Employee::Employee()
{
	name="";
	position="";
	age=0;
}
Employee::Employee(const Employee& E)
{
	name=E.name;
	position=E.position;
	age=E.age;
}
Employee::Employee(string n,string p,int a)
{
	name=n;
	position=p;
	age=a;
}

Employee & Employee::operator=(const Employee& E)
{
	name=E.name;
	position=E.position;
	age=E.age;
}

ostream & operator<< (ostream& s,const Employee& E)
{
	s << E.name << ", " << E.position << ", " << E.age << " years old.";
}


And my "Map.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

class Element_Not_Found{};

template <class A,class B> class Map
{
private:
	struct node
	{
		A key;
		B value;
		node* next;
		bool hasNext()
		{
			return (next!=NULL);
		}
	};

	node* head;
public:
	Map()
	{
		head = NULL;
	}

	Map(const Map<A,B>& m)
	{
		(*this)=m;
	}
	~Map()
	{
		while(head)
		{ 
			node* t=head->next;
			delete head;
			head=t;
		}
	}

	void add(A k, B val)
	{
		node *t = new node;
		t->next = head;
		head = t;
		head->value = val;
		head->key=k;
	}

	B* find(A k)
	{
		node* aux=head;
		B* ret;
		while (aux->hasNext())
		{
			if(aux->key==k)
			{
				ret = &aux->value;
				return ret;
			}
			aux=aux->next;
		}
		throw Element_Not_Found();
	}

	friend ostream & operator<< (ostream& s,const Map<A,B>& m)
	{
		node* aux=m.head;
		while (aux->hasNext())
		{
			s << aux->key << " : " << aux->value << endl ;
			aux = aux->next;
		}
	}

	Map& operator= (const Map<A,B>& m)
	{
		if (&m == this)
			return *this;
		while (head)
		{
			node *t = head->next;
			delete head;
			head = t;
		};
		node *src, **dst;
		head = NULL;
		src = m.head;
		dst = &head;
		while (src)
		{
			*dst = new node;
			(*dst)->value = src->value;
			(*dst)->key = src->key;
			(*dst)->next = NULL;
			src = src->next;
			dst = &((*dst)->next);
		}
		return *this;
	}
};



My problem is that I have a core dump after the 2 last added Employees had been printed.
So somehow it seems that I have a problem when I add my first Employee to the map, but I don't know what it is, and MOST OF ALL, I don't understand how the other 2 Employees can be printed

Thanks by advance
One of the problems is that you seem to have some functions that should return a value but don't.

Also, except for templates, a header file shouldn't contain executable code. Your employee class should be broken up into header file and implementation file.

Third why all the pointers in your main() function?

Also since your Map has pointers you probably need to do a deep copy instead of trying "assign" one Map to another.

Hopefully you have run the program with your debugger, which should tell you were it encountered the problem and then you can backtrack from there.
Thanks for you answer, I will try to do all these things and then come back if I still have problems.

Third why all the pointers in your main() function?


I don't see what you're talking about, there's only pE
I don't see what you're talking about, there's only pE

Yes and why the pointer?

And how many times does that pointer change?
The main function has been given to me by the teacher, my task is to make it work.

Isn't it still one pointer, even if it's re-assigned several times ?
My point is that using a pointer in C++ should be the last resort, especially a non-constant pointer.

By the way all that pointer changing is a big part of the problem because your functions are incorrect.

Your find() method won't find the item if it's the last one in the list.
Your << operator skips the last item too.

In both cases the problem is that you're using hasNext(). You don't care if a node has a next pointer, you only care whether the pointer to the node itself is nullptr.
Thank you so much :)
Topic archived. No new replies allowed.