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
136
137
138
139
140
141
#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;
}
// 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;

	}
// 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;
		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;
	}
// 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;
	}







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
Compiling...
3.cpp
C:\HW3\3.cpp(37) : error C2533: 'hash::hash' : constructors not allowed a return type
C:\HW3\3.cpp(59) : error C2065: 'table' : undeclared identifier
C:\HW3\3.cpp(59) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(60) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(60) : 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(61) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(61) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(61) : error C2227: left of '->next' must point to class/struct/union
C:\HW3\3.cpp(63) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(63) : error C2541: delete : cannot delete objects that are not pointers
C:\HW3\3.cpp(75) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(77) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(77) : 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(80) : error C2109: subscript requires array or pointer type
C:\HW3\3.cpp(80) : 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(82) : error C2181: illegal else without matching if
C:\HW3\3.cpp(86) : error C2601: 'insert' : local function definitions are illegal
C:\HW3\3.cpp(98) : error C2601: 'remove' : local function definitions are illegal
C:\HW3\3.cpp(124) : error C2601: 'getloadfactor' : local function definitions are illegal
C:\HW3\3.cpp(142) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

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


:((
Add a semicolon on line 35
table should probably be a member variable.
I also get errors saying that the reference to hash is ambiguous. It's because you use using namespace std; so it doesn't know if you mean ::hash or std::hash when you write hash.
Last edited on
thx peter for replaying .. i really don't understand ur last note .. i fixed wt u said but i still get those errors :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Compiling...
3.cpp
C:\HW3\3.cpp(61) : error C2440: '=' : cannot convert from 'int *' to 'class node *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\HW3\3.cpp(62) : error C2227: left of '->next' must point to class/struct/union
C:\HW3\3.cpp(78) : error C2440: 'initializing' : cannot convert from 'int *' to 'class node *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\HW3\3.cpp(83) : error C2181: illegal else without matching if
C:\HW3\3.cpp(87) : error C2601: 'insert' : local function definitions are illegal
C:\HW3\3.cpp(99) : error C2601: 'remove' : local function definitions are illegal
C:\HW3\3.cpp(125) : error C2601: 'getloadfactor' : local function definitions are illegal
C:\HW3\3.cpp(143) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

3.exe - 8 error(s), 0 warning(s)
Shouldn't table be of type node**?
the idea is to make an array of pointers & when a collision occer i solve it by chaining using link list ..do u have another idea ..i actually don't know how to make the table member can u help me with the code ?!
Topic archived. No new replies allowed.