i need a quick help plz..(Hash class)

im writing a HW a bout universal hashing H=((ak+b)%p)%n) with chaining
but i got a lot of errors & it's making me crazy plz help me to solve errors

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<math.h>
using namespace std;

class node{
public:
int id;
char * name;
char * address;
int age;
node * next;
int GPA;
node(int num,char * nam,char * addr,int ages,int gpa){
	id = num;
	name = nam;
	address = addr;
	age = ages;
	GPA = gpa;}

};

class hash{

public : hash();
		 hash(int aa,int bb,int prime,int size);
		 ~hash();
		 int hashfunc(int key);
		 bool insert(node *n);
		 bool retrieve(int id2,node *nn);
		 bool remove(int id3);
		 double getloadfactor();
 int a ;int b;int p; int n;
}

hash::hash(){
	a=23;b=88,n=100,p=997;
	int * *table;
	for(int i=0;i<n;i++){
	table [i]=NULL;}

}

hash::hash(int aa,int bb,int prime,int size){

	a=aa;b=bb;p=prime;n=size;
	int **table;
	for( int i=0;i<n;i++){
	table [i]=NULL;}

}
hash::~hash(){
	node *p;
	for(int j=0;j<n;j++)
		while (table[j]!=NULL){
				p=table[j];
			table[j]=table[j]->next;
			delete p;}
			delete []table[j];
}

int hash::hashfunc(int key){
	int H=((a*key+b)%p)%n;
	return H;
}

bool hash::retrieve(int id2,node *nn){
int s=hashfunc(id2);
bool done=true;
if(table[s]==NULL)done=false;

else{node *pp=table[s];
while(pp!=NULL && !done){
	if(pp->id==id2)
	{memcpy(nn,table[s],sizeof(node));
	done = true;
		else pp=pp->next;

	}

	bool insert(node *n){
		int F=hash(n->id);
		if(table[F]==NULL){
			table[F]=n;
			done=true;}
		else{
			n->next=table[F];
			table[F]=n;
		}
		return done;
	}

	bool hash::remove(int id3){
		node *p1,*p2;
		p1=p2=table[m];
		int m=hashfunc(id3);
		if(table[m]==NULL)done=true;

		else if(table[m]->id==id3){
			node *v=table[m];
			table[m]=table[m]->next;
			delete v;
			done =true;
		}
		else {
			while(p2!=NULL){
				if(p2->id==id3){
					p1->next=p2->next;
					delete p2;
					done = true;
				}
				else{ p1=p2;
				p2=p2->next;}
			}
		}
		return done;
	}

	double hash::getloadfactor(){
		double count=0;
		for(int u=0;u<n;u++){
			if(table[u]!=NULL){
				node *e=table[u];
				while(e!=NULL){
					count++;
					p=p->next;}
			}
		}
		return count/size;
	}







here the errors i got

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
3.cpp
C:\HW3\3.cpp(35) : error C2533: 'hash::hash' : constructors not allowed a return type
C:\HW3\3.cpp(54) : error C2065: 'table' : undeclared identifier
C:\HW3\3.cpp(54) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(55) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(55) : error C2440: '=' : cannot convert from 'int' to 'class node *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\HW3\3.cpp(56) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(56) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(56) : error C2227: left of '->next' must point to class/struct/union
C:\HW3\3.cpp(58) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(58) : error C2541: delete : cannot delete objects that are not pointers
C:\HW3\3.cpp(69) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(71) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(71) : error C2440: 'initializing' : cannot convert from 'int' to 'class node *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\HW3\3.cpp(74) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(74) : error C2664: 'memcpy' : cannot convert parameter 2 from 'int' to 'const void *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\HW3\3.cpp(76) : error C2181: illegal else without matching if
C:\HW3\3.cpp(80) : error C2601: 'insert' : local function definitions are illegal
C:\HW3\3.cpp(92) : error C2601: 'remove' : local function definitions are illegal
C:\HW3\3.cpp(118) : error C2601: 'getloadfactor' : local function definitions are illegal
C:\HW3\3.cpp(136) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

HW3.exe - 20 error(s), 0 warning(s)


:(
You're defining functions inside another functions in lines 80-128.
mmm i fix it like that but i still have the same 20 errors :(

bool hash::insert(node *n){
You're missing a semicolon on the end of your hash class declaration.
?!

Ohhh buddy, haven't seen something like this , but here are a list of things you missed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class hash{
public : 
    hash();
    hash(int aa,int bb,int prime,int size);
    ~hash();
    int hashfunc(int key);
    bool insert(node *n);
    bool retrieve(int id2,node *nn);
    bool remove(int id3);
    double getloadfactor();
    int a ;
    int b;
    int p;
    int n;
}*;*// you need a ;  

1
2
3
4
5
6
7
8
9
10
hash::hash(){
    a=23;
    b=88//*,*//
    n=100//*,*// you need to end statement with ';' not ','
    p=997;
    int * *table;
    for(int i = 0 ; i<n ; i++){
        table [i]=NULL;
    }
}

1
2
3
4
5
6
7
8
9
10
11
hash::~hash(){
    node *p;
    for(int j=0;j<n;j++)
	while (table[j]!=NULL){
	    p=table[j];
	    table[j]=table[j]->next;
	    delete p;
	}
    delete[]table // *[j]*// unless your table is 2d array,which is pointing to an array of arrays 
                         // you delete[] table[j] , otherwise you only "delete[] table;"
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// I don't know if you meant to write it like this, if you did your missing '}'.
bool hash::retrieve(int id2,node *nn){
    int s=hashfunc(id2);
    bool done=true;
    if(table[s]==NULL)
	done=false;

    else{
      	node *pp=table[s];
	while(pp!=NULL && !done){
	    if(pp->id==id2){
		memcpy(nn,table[s],sizeof(node));
		done = true;
		else pp=pp->next;
            }
	//*}*// close while
   // *}*// close else 
//*}*// close function 

1
2
3
4
5
6
7
8
9
10
11
12
//bool *hash::*insert(node *n){// you are missing class identifier here "hash::"
    int F= hash(n->id);
    if(table[F]==NULL){
	table[F]=n;
	done=true;
    }
    else{
	n->next=table[F];
	table[F]=n;
    }
    return done;
}


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
bool hash::remove(int id3){
    node *p1,*p2;
    p1=p2=table[m];
    int m=hashfunc(id3);
    if(table[m]==NULL)
    	done=true;

    else if(table[m]->id==id3){
	node *v=table[m];
	table[m]=table[m]->next;
	delete v;
	done =true;
    }
    else{
	while(p2!=NULL){
	    if(p2->id==id3){
		p1->next=p2->next;
	        delete p2;
		done = true;
	    }
            else{ 
   	        p1=p2;
	        p2=p2->next;
	    }
         *}*// you forgot to close while
    }
}
    *return done;*// you left your return statement out of the function 


another error:
1
2
3
4
5
6
7
8
9
hash::~hash(){
	node *p;
	for(int j=0;j<n;j++)
		while (table[j]!=NULL){ // here table is not part of your class data member it is unidentified 
				p=table[j];
			table[j]=table[j]->next;
			delete p;}
			delete []table[j];
}
Last edited on
i fixed all the things u notice but i still got a lot of errors .. can u help me with making table a member i don't know wt to do :(

here my program after fixing
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<iostream>
#include<math.h>
using namespace std;

// class node
class node{
public:
int id;
char * name;
char * address;
int age;
node * next;
int GPA;
node(int num,char * nam,char * addr,int ages,int gpa){
	id = num;
	name = nam;
	address = addr;
	age = ages;
	GPA = gpa;}

};

// class Hash
class hash{

public : hash();
		 hash(int aa,int bb,int prime,int size);
		 ~hash();
		 int hashfunc(int key);
		 bool insert(node *n);
		 bool retrieve(int id2,node *nn);
		 bool remove(int id3);
		 double getloadfactor();
 int a ;int b;int p; int n;
 	int **table;
};
// constructers
hash::hash(){
	a=23;b=88;n=100;p=997;
	int * *table;
	for(int i=0;i<n;i++){
	table [i]=NULL;}

};

hash::hash(int aa,int bb,int prime,int size){

	a=aa;b=bb;p=prime;n=size;
	int **table;
	for( int i=0;i<n;i++){
	table [i]=NULL;}

};

//destructers

hash::~hash(){
	node *p;
	for(int j=0;j<n;j++)
		while (table[j]!=NULL){
				p=table[j];
			table[j]=table[j]->next;
			delete p;}
			delete []table[j];
};

// hashing function
int hash::hashfunc(int key){
	int H=((a*key+b)%p)%n;
	return H;
}
// retrieve function
bool hash::retrieve(int id2,node *nn){
int s=hashfunc(id2);
bool done=true;
if(table[s]==NULL)done=false;

else{
	node *pp=table[s];
while(pp!=NULL && !done){
	if(pp->id==id2)
	{
		memcpy(nn,table[s],sizeof(node));
		done = true;}
	else{ pp=pp->next;}
}
}
return done;
	}

// insert function
	bool hash::insert(node *n){
		int F=hash(n->id);
		if(table[F]==NULL){
			table[F]=n;
			done=true;}
		else{
			n->next=table[F];
			table[F]=n;
		}
		return done;
	}
// remove function
	bool hash::remove(int id3){
		node *p1,*p2;
		
		int m=hashfunc(id3);
		p1=p2=table[m];
		if(table[m]==NULL)done=true;

		else if(table[m]->id==id3){
			node *v=table[m];
			table[m]=table[m]->next;
			delete v;
			done =true;
		}
		else {
			while(p2!=NULL){
				if(p2->id==id3){
					p1->next=p2->next;
					delete p2;
					done = true;
				}
				else{ p1=p2;
				p2=p2->next;}
			}
		}
	
		return done;
	}

// load function
	double hash::getloadfactor(){
		double count=0;
		for(int u=0;u<n;u++){
			if(table[u]!=NULL){
				node *e=table[u];
				while(e!=NULL){
					count++;
					p=p->next;}
			}
		}
		return count/size;
	}






can you please post your errors ?

-TAZO
here are more corrections:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// class Hash
class hash{
public : 
    hash();
    hash(int aa,int bb,int prime,int size);
    ~hash();
    int hashfunc(int key);
    bool insert(node * n);
    bool retrieve(int id2,node * nn);
    bool remove(int id3);
    double getloadfactor();
    int a;
    int b;
    int p; 
    int n;
    node * table; // your table is pointing to a table(array) of nodes pointers (not int)
};

1
2
3
4
5
6
7
8
9
10
11
// constructers
hash::hash(){
    a=23;
    b=88;
    n=100;
    p=997;
    // table = new node * [n]; // here we want to create (allocate) and array of node pointers
                        // and since n = size of the pointer table.
    for(int i = 0; i < n;i++)
	table[i]=NULL;
};

1
2
3
4
5
6
7
8
9
hash::hash(int aa,int bb,int prime,int size){
    a=aa;
    b=bb;
    p=prime;
    n=size;
    table = new node * [n]; // same here n is the size , and we are asking for new table of node pointers 
    for( int i = 0 ;i < n;i++)
	table[i]=NULL;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//destructers

hash::~hash(){

    delete[] table; // we only delete table becuase only table allocated new memory.
		    // which was the array of node pointers.
} // new dTor

    /*node * p;
    for(int j = 0; j < n;j++){
        while (table[j]!=NULL){
	    p=table[j];
	    table[j]=table[j]->next;
	    delete p;
  	}
    }
    delete[]table[j];
};*/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// insert function
bool hash::insert(node * n){
    int F = //hashfunc(n->id); // you need to call you hash function (not cTor)
    if(table[F]==NULL){
	table[F] = n;
	done = true; // done is unidentified (you need to declare and define it).
    }
    else{
        //n->next = NULL; // add this line to make sure next is not pointing at garbage
	n->next = table[F]; // do the opposite table[F]->next = n;
	table[F]=n; // here you just overwrote over the old node... Delete this line.
	// add a line that sets done to true, because at this point you added the node.
    }
    return done;// done is unidentified (you need to declare and define it).
}

Last edited on
thx TAZO .. i fixed my code but i changed ur first note
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


// class Hash
class hash{

public : hash();
		 hash(int aa,int bb,int prime,int size);
		 ~hash();
		 int hashfunc(int key);
		 bool insert(node *n);
		 bool retrieve(int id2,node *nn);
		 bool remove(int id3);
		 double getloadfactor();
         int a ;int b;int p; int n;
 	node **table;
};


i put node **table insted of node * table

so i got no errors but in the main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int main(){
	
int gb=5; int gb1=7;int g=17;int num=9; int num1=5; int g1=12;
char *b="abdallah";
char *c="holyland";
char *b1="omar";
char *c1="america";
node d(num,b,c,g,gb);
node f(num1,b1,c1,g1,gb1);
int a1=1,b2=0,p1=5,n1=13;
hash i(a1,b2,p1,n1);
i.insert(d);
i.insert(f);
	
	return 0;
	}


while testing i got those errors & i think there's a problem in insert
1
2
3
4
5
6
7
8
9
10
Compiling...
3.cpp
C:\HW3\3.cpp(169) : error C2664: 'insert' : cannot convert parameter 1 from 'class node' to 'class node *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\HW3\3.cpp(170) : error C2664: 'insert' : cannot convert parameter 1 from 'class node' to 'class node *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

3.exe - 2 error(s), 0 warning(s)


here is my program after fixing

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<iostream>
#include<math.h>
using namespace std;

// class node
class node{
public:
int id;
char * name;
char * address;
int age;
node * next;
int GPA;
node(int num,char * nam,char * addr,int ages,int gpa){
	id = num;
	name = nam;
	address = addr;
	age = ages;
	GPA = gpa;}

};



// class Hash
class hash{

public : hash();
		 hash(int aa,int bb,int prime,int size);
		 ~hash();
		 int hashfunc(int key);
		 bool insert(node *n);
		 bool retrieve(int id2,node *nn);
		 bool remove(int id3);
		 double getloadfactor();
         int a ;int b;int p; int n;
 	node **table;
};
// constructers

hash::hash(){
	a=23;b=88;n=100;p=997;

    table=new node *[n];
	for(int i=0;i<n;i++){
	table [i]=NULL;}

};

hash::hash(int aa,int bb,int prime,int size){

	a=aa;b=bb;p=prime;n=size;
	table=new node *[n];
	for( int i=0;i<n;i++){
	table [i]=NULL;}

};

//destructers

hash::~hash(){

delete[] table;

/*	node *p;
	for(int j=0;j<n;j++)
		while (table[j]!=NULL){
				p=table[j];
			table[j]=table[j]->next;
			delete p;}
			delete []table[j]; */
};

// hashing function
int hash::hashfunc(int key){
	int H=((a*key+b)%p)%n;
	return H;
}
// retrieve function
bool hash::retrieve(int id2,node *nn){
int s=hashfunc(id2);
bool done=true;
if(table[s]==NULL)done=false;

else{
	node *pp=table[s];
while(pp!=NULL && !done){
	if(pp->id==id2)
	{
		memcpy(nn,table[s],sizeof(node));
		done = true;}
	else{ pp=pp->next;}
}
}
return done;
	}

// insert function
	bool hash::insert(node *n){
		bool done=false;
		int F=hashfunc(n->id);
		if(table[F]==NULL){
			table[F]=n;
			done=true;}
		else{
			n->next=NULL;
			table[F]=n;
			done=true;
			
		}
		return done;
	}
// remove function
	bool hash::remove(int id3){
		node *p1,*p2;
		bool done;
		int m=hashfunc(id3);
		p1=p2=table[m];
		if(table[m]==NULL)done=true;

		else if(table[m]->id==id3){
			node *v=table[m];
			table[m]=table[m]->next;
			delete v;
			done =true;
		}
		else {
			while(p2!=NULL){
				if(p2->id==id3){
					p1->next=p2->next;
					delete p2;
					done = true;
				}
				else{ p1=p2;
				p2=p2->next;}
			}
		}
	
		return done;
	}

// load function
	double hash::getloadfactor(){
		double count=0;
		for(int u=0;u<n;u++){
			if(table[u]!=NULL){
				node *e=table[u];
				while(e!=NULL){
					count++;
					e=e->next;}
			}
		}
		return count/n;
	}



	int main(){
	
int gb=5; int gb1=7;int g=17;int num=9; int num1=5; int g1=12;
char *b="abdallah";
char *c="holyland";
char *b1="omar";
char *c1="america";
node d(num,b,c,g,gb);
node f(num1,b1,c1,g1,gb1);
int a1=1,b2=0,p1=5,n1=13;
hash i(a1,b2,p1,n1);
i.insert(d);
i.insert(f);
	
	return 0;
	}




can u help me with that one plz
again when you say node ** table, "what you are saying is the thing that table is pointing to"
which is the actual node, you table suppose to act like a pointer to a node rather then the
actual data..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool hash::insert(node * n){
    bool done = false;
    int F = hashfunc(n->id);
    if(table[F] == NULL){
        table[F] = n; // here is the problem, your n is a pointer to a node and your table 
		      // is a node in your program, so what your doing is assigning 'n' 
                      // to its-self. (n == node *) where ( table == node ).
		      // not only that, you are also saying that your node is an array by saying
                      // table[F] ...
    done = true;
    }
    else{
        n->next=NULL;
        table[F] = n; // same here 
        done = true;
    }
    return done;
}
i think i must convert table to array of pointers not node but the problem is i dont know how to do that with making it a member too :(

i really feel suck of this code it never fix .. & i have to send this HW to my doctor by tomorrow..
that's so frustrating
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class hash{

public : hash();
		 hash(int aa,int bb,int prime,int size);
		 ~hash();
		 int hashfunc(int key);
		 bool insert(node *n);
		 bool retrieve(int id2,node *nn);
		 bool remove(int id3);
		 double getloadfactor();
         int a ;int b;int p; int n;
 	node **table; //all you need to do here is type " node * table;"
};
// constructers 


AND then here

1
2
3
4
5
6
7
8
9
10
11
// it is in the constructer where table points to an array of node pointers.
hash::hash(){
    a=23;
    b=88;
    n=100;
    p=997;
    table = new node * [n]; // here we want to create (allocate) and array of node pointers
                        // and since (n = size of the pointer table) we allocate n size of array.
    for(int i = 0; i < n;i++)
	table[i]=NULL;
};
i fixed it in another way thx sooooooooo much TAZO :)
Topic archived. No new replies allowed.