hashTable.h:
Code:
#ifndef HASHTABLE_H
#define HASHTABLE_H
struct wordCount
{
char *word;
int count;
};
typedef struct wordCount HT_Element;
typedef struct wordCount *HT_Element_Ptr;
// Paul22000: Removed A LOT of comments for each method below for simplicity:
extern void HT_initialize(int n);
extern void HT_cleanup();
extern HT_Element_Ptr HT_lookup(char *s);
extern HT_Element_Ptr HT_install (char *word);
extern int HT_getNrOfSlots();
extern int HT_getNrOfElements();
extern HT_Element HT_getFirstElement();
extern HT_Element HT_getNextElement();
#endif
-----
main.c:
(Literally commented everything out for this post, to show errors)
Code:
#include <stdio.h>
#include <string.h>
#include "hashTable.c"
int main()
{
// commented everything out, still get errors
return 0;
}
-----
The Errors:
Code:
1>------ Build started: Project: Project2, Configuration: Debug Win32 ------
1>Linking...
1>countWords.obj : error LNK2005: _HT_initialize already defined in hashTable.obj
1>countWords.obj : error LNK2005: _HT_lookup already defined in hashTable.obj
1>countWords.obj : error LNK2005: _hash already defined in hashTable.obj
1>C:\Documents and Settings\Paul\My Documents\Visual Studio 2008\Projects\countWords\Debug\Project2.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\Paul\My Documents\Visual Studio 2008\Projects\countWords\countWords\Debug\BuildLog.htm"
1>Project2 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And here's hashTable.c, it doesn't really help, which is why I put it last in the post, so feel free to ignore this completely.
(Ignore all the crap going on in the functions, I'm in the middle of implementing them but having quit a lot of trouble)
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef HASHTABLE_H
#define HASHTABLE_H
#define HASHSIZE 50000
/* =================================
* Type Declarations:
*/
unsigned hash(char *s);
struct wordCount
{
char *word;
int count;
};
typedef struct wordCount HT_Element;
typedef struct wordCount *HT_Element_Ptr;
struct llistelem
{
HT_Element hte;
struct llistelem *next;
};
typedef struct llistelem LL_Element;
LL_Element *myhashtable;
/* ==============================================================
* Function Declarations:
*/
void HT_initialize(int n)
{
myhashtable = (LL_Element *)calloc(n, sizeof(LL_Element));
}
extern void HT_cleanup();
/////////////////////////////////////////////////////////////////////
// Ignore all this crap, I'm in the middle of implementing these functions, and have no idea how to do it
HT_Element_Ptr HT_lookup(char *s)
{
//np = &myhashtable[0];
//printf("test1: %s\n", np->hte.word);
LL_Element *np = (LL_Element *)calloc(1, sizeof(LL_Element));
int x = 0;
for (np = &myhashtable[hash(s)]; np != NULL; np = np->next)
{
printf("%s\n", np->hte.word);
x++;
}
//if (strcmp(s, np->hte.word) == 0)
//return NULL;//&np; /* found */
return NULL; /* not found */
}
extern HT_Element_Ptr HT_install (char *word);
extern int HT_getNrOfSlots();
extern int HT_getNrOfElements();
extern HT_Element HT_getFirstElement();
extern HT_Element HT_getNextElement();
unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
#endif /* HASHTABLE_H */