So I am to take this code for a hash table from a book and I am to read in the contents of a dictionary and store them in this hash table. I can't figure out how to read in all the words without getting a segmentation fault. Any ideas? Any guidance is much appreciated. Here is my code below.
Could you explain a little bit more on that? I got it running without everything between lines 77-90 in the hash.cpp, it just doesn't quite do what I want it to.
And I believe the (void) casting just tells the function not to use the return value.
"This looks like a bunch of old-timey C code dressed up with C++ headers"
is what I was going to say, until I saw the comment at the top. Okay, I nust have reflexively blocked that out at the point of visual impact.
I imagine these are just copy-paste errors, but here goes:
staticchar *Strdup( constchar * ); // in cstring, but.... no, it's not. C and C++ are case sensitive. Strdup is not the same as strdup.
1 2 3 4
while(!infile.eof())
{
infile >> name;
}
Do you really want just the last word in the file?
1 2 3 4 5 6
NListPtr Insert( char *name, char *defn )
...
(void)Insert( "One", "1" );
// "One" and "1" are of type const char *
// Insert expects 2 arguments of type char *
// You cannot mix the 2 this way
1 2 3 4 5 6 7 8 9 10
???
/* hash.h
*/
#include<iostream>
usingnamespace std; //??? in a header
error: ‘hashTable’ was not declared in this scope
TestHash.cpp: I
#ifndef HASH_H
I think this is definitely a copy and paste error
#include "hash.cpp" ?? don't defeat the point of separate compilation
hashVal is used uninitialised in Insert()
Could you post your newer, working code? Also do you really want to do this in C or in C++?
A string literal like "One" is a constchar*
You are passing a constchar* to a function that expects a char* (`Insert()')
There you just aimed the gun at your foot.
In that function you do infile >> name;
So you are trying to modify a string literal, a constchar*
There you've pulled the trigger.