Im trying to make this Hash Table to work, but I get these errors on linking time

main.obj : error LNK2019: unresolved external symbol "public: __thiscall List::~List(void)" (??1List@@QAE@XZ) referenced in function "public: __thiscall HTable::~HTable(void)" (??1HTable@@QAE@XZ)

StringTable.obj : error LNK2001: unresolved external symbol "public: __thiscall List::~List(void)" (??1List@@QAE@XZ)

HashTable.obj : error LNK2019: unresolved external symbol "public: void __thiscall List::Add(int)" (?Add@List@@QAEXH@Z) referenced in function "public: void __thiscall HTable::Add(char const *,int)" (?Add@HTable@@QAEXPBDH@Z)

main.exe : fatal error LNK1120: 2 unresolved externals

this is the body of those functions

//HashTable.cpp
List const & HTable::Find (char const * str) const
{
int i = hash (str);
return _aList [i];
}

void HTable::Add (char const * str, int id)
{
int i = hash (str);
_aList[i].Add (id);
}

They use class List wich I have included on header file HashTable.h. List.h and List.obj are in another folder "..\LinkedListFolder" and the other obj are inside main folder so I issued:


//Im using Visual C++

cl main.obj *.obj -libpath:..\LinkedListFolder"

Lemme explain hierarchy:
sLink inherits from Link
I use sLink to implement the List
and List to implement Hash Table

//link.h
class Link
{
public:
Link (int id): _id (id) {}
virtual ~Link();
int Id () const { return _id; }
private:
int _id;
};
//sLink.h
#include "Link.h"
class sLink : public Link
{
public:
  sLink (sLink* pNext, int id): Link(id), _pNext (pNext)   {}
  sLink * Next () const { return _pNext; }
private:
  sLink * _pNext;
};
List.h
#include "sLink.h"
class List
{
public:
List (): _pHead (NULL){}
~List ();
void Add ( int id );
sLink const * GetHead () const { return _pHead; }
private:
sLink * _pHead;
};
//List.cpp
#include "sList.h"

List::~List ()
{
// free the list
while ( _pHead != NULL )
{
sLink* pLink = _pHead;
_pHead = _pHead->Next(); // unlink pLink
delete pLink;
}
}

void List::Add ( int id )
{
// add in front of the list
sLink * pLink = new sLink (_pHead, id);
_pHead = pLink;
}
//HashTable.h
#include "sList.h"

const int sizeHTable = 127;

// Hash table of strings
class HTable
{
public:
List const & Find (char const * str) const;// return a short list of candidates
void Add (char const * str, int id);// add another string->id mapping
private:
int hash (char const * str) const;  // the hashing function
List _aList [sizeHTable]; // an array of (short) lists
};

BTW Could you clarify me if in fact destructor of sList on cpp is called or is it assuming empty constructor due to declaration of destructor on header file.

So why the linking errors?

Follow your includes. HashTable.h includes sList.h, but I don't see an sList.h. I do see a List.h, though. Same in List.cpp, you include sList.h instead of List.h.

yea sry It was a typo when I was making thread I didnt cut and paste documents name the correct name for List.h is sList.h

This is an excersice from the book "C++ In action" you can google for the book title and it will take you to its official site where you can find the book also the source Code, I will look at it to see my mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.