-
help!!! again!!!
ok, this is somewhat a continuation of my last post,
i had to write a project using templates. well my dumbass didnt write the project first then modify it to handle templates
so i need help from anyone debugging this sucker
----------------------------------------------------------------------------------
errors:
----------------------------------------------------------------------------------
/usr/local/bin/g++ -ansi -Wall -c proj5.cpp HashMap.cpp HashMapException.cpp
proj5.cpp:2: warning: `/*' within comment
proj5.cpp:22: warning: `/*' within comment
HashMap.cpp: In method `HashMap::HashMap(int (*)(int), int)':
HashMap.cpp:25: request for member `next' in `*bucketnode', which is of non-aggr
egate type `element *'
HashMap.cpp:26: request for member `key' in `*bucketnode', which is of non-aggre
gate type `element *'
HashMap.cpp:27: request for member `value' in `*bucketnode', which is of non-agg
regate type `element *'
HashMap.cpp: In method `HashMap::~HashMap()':
HashMap.cpp:37: no match for `element & = int'
HashMap.h:18: candidates are: struct element & element:perator =(const element
&)
HashMap.cpp: In method `bool HashMap::Insert(int, int)':
HashMap.cpp:50: `struct element' has no member named `data'
HashMap.cpp:51: `buckets' undeclared (first use this function)
HashMap.cpp:51: (Each undeclared identifier is reported only once
HashMap.cpp:51: for each function it appears in.)
HashMap.cpp: At top level:
HashMap.cpp:56: `KeyType' was not declared in this scope
HashMap.cpp:56: parse error before `)'
HashMap.cpp:57: prototype for `bool HashMap::Remove(...)' does not match any in
class `HashMap'
HashMap.h:33: candidate is: bool HashMap::Remove(int)
HashMap.cpp: In method `bool HashMap::Remove(...)':
HashMap.cpp:60: `key' undeclared (first use this function)
HashMap.cpp: In method `bool HashMap::Replace(int, int)':
HashMap.cpp:93: warning: control reaches end of non-void function `HashMap::Repl
ace(int, int)'
HashMap.cpp: In function `class ostream & operator <<(ostream &, const HashMap &
)':
HashMap.cpp:129: `bucket' undeclared (first use this function)
HashMap.cpp:129: base operand of `->' has non-pointer type `const HashMap'
HashMap.cpp:132: warning: control reaches end of non-void function `operator <<(
ostream &, const HashMap &)'HashMap.cpp: In method `void HashMap::PrintBucket(int) const':
HashMap.cpp:136: initialization to `int' from `int (*)(int)' lacks a cast
HashMap.cpp:137: invalid types `element *const[int (*)(int)]' for array subscrip
t
HashMap.cpp:151: no match for `element & = element *&'
HashMap.h:18: candidates are: struct element & element:perator =(const element
&)
make: *** [proj5] Error 1
----------------------------------------------------------------------------------
hashmap.h
----------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
using std::setw;
using std::string;
using std::cin;
using std::cout;
using std::cerr;
struct element
{
element *next;
int value;
int key;
};
class HashMap
{
public:
typedef int (*hashfunc) ( int key );
HashMap( hashfunc h, int buckets );
~HashMap( );
bool Insert ( int key, int value);
bool Remove ( int key );
bool Replace ( int key, int value);
int Find ( int key ) const;
bool Has ( int key ) const;
friend ostream& operator<< ( ostream& o, const HashMap& hm);
void PrintBucket ( int key ) const;
private:
element *bucket;
int numbuckets;
};
----------------------------------------------------------------------------------
hashmap.cpp
----------------------------------------------------------------------------------
#include <string>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
//using std::setw;
//using std::string;
//using std::cin;
//using std::cout;
//using std::cerr;
#include "HashMap.h"
#include "HashMapException.h"
HashMap::HashMap(hashfunc h, int _numbuckets)
{
numbuckets=_numbuckets;
if (numbuckets <= 0)
{
string s1 = "HashMap Constructor";
throw HashMapException(s1);
}
element **bucketnode;
for ( int i = 0; i <= numbuckets; i++ )
{
bucketnode->next[i] = NULL;
bucketnode->key[i] = NULL;
bucketnode->value[i] = NULL;
}
}
HashMap::~HashMap( )
{
for ( int i = 0; i <= numbuckets; i++)
{
bucket[i] = NULL;
}
delete [] bucket;
}
bool HashMap::Insert ( int key, int value)
{
element *newptr = new element;
if (newptr == NULL)
{
return false;
}
newptr -> data = value;
newptr -> next = buckets[hashfunc(key)];
buckets[hashfunc(key)] = newptr;
return true;
}
bool HashMap::Remove ( KeyType key )
{
element *front;
element *back;
front = buckets[hashfunc(key)];
back = front;
if (front -> key == key)
{
delete front;
return true;
}
while (front != NULL)
{
if (front -> key == key)
{
back -> next = front -> next;
delete front;
return true;
}
front = front -> next;
}
return false;
}
bool HashMap::Replace ( int key, int value)
{
element *runner = buckets[hashfunc(key)];
while (runner != NULL)
{
if (runner -> key == key)
{
runner -> value = value;
return true;
runner = runner -> next;
}
return false;
}
}
int HashMap::Find ( int key ) const
{
element *runner = buckets[hashfunc(key)];
while (runner != NULL)
{
if (runner -> key == key)
{
return runner -> value;
}
runner = runner -> next;
}
string s1 = "Find";
throw HashMapException (s1);
}
bool HashMap::Has ( int key ) const
{
element *runner = buckets[hashfunc(key)];
while (runner != NULL)
{
if (runner -> key == key)
{
return true;
}
runner = runner -> next;
}
return false;
}
ostream& operator<< ( ostream& o, const HashMap& hm)
{
for (int i = 0; i < buckets; i++)
{
bucket[i].PrintBucket(hm->key);
}
}
void HashMap::PrintBucket ( int key ) const
{
int bucketnum = hashfunc(key);
element *runner = bucket[hashfunc(key)];
if (runner == NULL)
{
cout << "Bucket " << bucketnum << " is empty" << endl;
return;
}
cout << "Bucket " << bucketnum << " contains: " << endl;
while (runner != NULL)
{
if (runner -> key == key)
{
cout << "(" << runner -> key << ", " << runner -> value
<< endl;
return;
}
*runner = runner -> next;
}
string s1 = "PrintBucket";
throw HashMapException (s1);
}
-
i give up, there is no way im making deadline anyway, anyone need a rather ****ty programmer, i should be avalible soon...hehe =P
thanks for trying everyone
Popular pages
Recent additions