Hi I'm Josh and I'm new to these forums, and new to c++. I hope that I can get some great hints and help from the community. thank you in advance!

I'm trying to create and use a hashtable class that uses a linked list for separate chaining. I've completed the code but I can't declare the class without generating these errors:

Hashing error LNK2019: unresolved external symbol "public: __thiscall hashTable<class employeeType>::~hashTable<class employeeType>(void)" (??1?$hashTable@VemployeeType@@@@QAE@XZ) referenced in function _main

Hashing error LNK2019: unresolved external symbol "public: __thiscall hashTable<class employeeType>::hashTable<class employeeType>(class employeeType const &,int)" (??0?$hashTable@VemployeeType@@@@QAE@ABVemployeeType@@H@Z) referenced in function _main

This is the main program:

// Hashing.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	employeeType ITEM_NOT_FOUND("none",-100,-100);
	hashTable<employeeType> test(ITEM_NOT_FOUND);
	return 0;
}

here is the header for the hashtable class:

#pragma once
#include ".\linkedList.h"
#include <vector>
using namespace std;

template<class hashedObject>
class hashTable
{
public:
	hashTable();
	//constructor, requires ITEM_NOT_FOUND, and hashtable size
	explicit hashTable(const hashedObject &notFound, int size = 101);
    hashTable(const hashTable &rhs)
	: ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND),theLists(rhs.listArray) {}

	//default destructor
	~hashTable(void);

	//empty the hashtable
	void makeEmpty();
	//insert an element into the hashtable
	void insert(const hashedObject& insertObject);
	//remove an item from the hash table
	void remove(const hashedObject& deleteObject);
	//retrieve an item from the table
	const hashedObject& retrieve(const hashedObject &findObject);

	const hashTable & operator=(const hashTable & rhs);

private:
	vector<linkedListType<hashedObject> > listArray;
	const hashedObject ITEM_NOT_FOUND;
};

//functions to hash the key
template<class hashedObject>
int hash(const hashedObject& key, int tableSize);

and here is the implementation for the hash table:

#include "StdAfx.h"
#include ".\hashtable.h"

//overload assignment operator
template <class hashedObject>
const hashTable<hashedObject> & hashTable<hashedObject>::operator=( const hashTable<hashedObject> & rhs )
{
	if( this != &rhs )
        listArray = rhs.listArray;
    return *this;
}

template<class hashedObject>
hashTable<hashedObject>::hashTable()
{
}

//Constructor
template<class hashedObject>
hashTable<hashedObject>::hashTable(const hashedObject &notFound, int size)
: ITEM_NOT_FOUND(notFound),listArray(nextPrime(size)) {}

//default destructor
template<class hashedObject>
hashTable<hashedObject>::~hashTable(void) {}

//delete all lists in each slot in the hash table
template<class hashedObject>
void hashTable<hashedObject>::makeEmpty()
{
	for (int i = 0; i < listArray.size(); i++)
		listArray[i].makeEmpty();
}

//delete an item from the hash table
template<class hashedObject>
void hashTable<hashedObject>::remove(const hashedObject &deleteObject)
{
	int hashIndex = hash(deleteObject,listArray.size());
	if (listArray[hashIndex].search(deleteObject))
        listArray[hash(deleteObject,listArray.size())].deleteElement(deleteObject);
}

//search for an item and retrieve it, return ITEM_NOT_FOUND if not found
template<class hashedObject>
const hashedObject &hashTable<hashedObject>::retrieve(const hashedObject &findObject)
{
	int hashIndex = hash(findObject,listArray.size());
	if (listArray[hashIndex].search)
		listArray[hashIndex].retrieve(findObject);
}

//insert an item into hashtable
template<class hashedObject>
void hashTable<hashedObject>::insert(const hashedObject &deleteObject)
{
	linkedListType<hashedObject> & whichList = listArray[hash(deleteObject,listArray.size())];
	if (!whichList.search(deleteObject))
		whichList.insertFront(deleteObject);
}

I thought maybe there was a problem with my employeeType class, but when I tried declaring

hashTable<int> test(ITEM_NOT_FOUND);

it didn't work either... what did I do wrong? and more importantly, how can I avoid making this mistake again? thank you for your time!

>Hi I'm Josh
Hi, Josh, I'm Julienne.

>Hashing error LNK2019: unresolved external symbol "public: __thiscall
>hashTable<class employeeType>::~hashTable<class employeeType>(void)"
>(??1?$hashTable@VemployeeType@@@@QAE@XZ) referenced in function _main
>
>Hashing error LNK2019: unresolved external symbol "public: __thiscall
>hashTable<class employeeType>::hashTable<class employeeType>(class employeeType const &,int)"
>(??0?$hashTable@VemployeeType@@@@QAE@ABVemployeeType@@H@Z) referenced in function _main
You've implemented the constructors and destructor, so my initial diagnosis is that you aren't linking with the implementation object file. What compiler are you using and how are you compiling the application?

>Hi I'm Josh
Hi, Josh, I'm Julienne.

>Hashing error LNK2019: unresolved external symbol "public: __thiscall
>hashTable<class employeeType>::~hashTable<class employeeType>(void)"
>(??1?$hashTable@VemployeeType@@@@QAE@XZ) referenced in function _main
>
>Hashing error LNK2019: unresolved external symbol "public: __thiscall
>hashTable<class employeeType>::hashTable<class employeeType>(class employeeType const &,int)"
>(??0?$hashTable@VemployeeType@@@@QAE@ABVemployeeType@@H@Z) referenced in function _main
You've implemented the constructors and destructor, so my initial diagnosis is that you aren't linking with the implementation object file. What compiler are you using and how are you compiling the application?

hi julienne(lovely name)! Thank you for answering my post!! I'm using visual studio .net 2003 academic. I tried copying all the stuff from the cpp and pasting it to the bottom of the .h file and it works!! but, WHY!!! why can't I have the files separate? or rather, what is the proper way to do it?

>(lovely name)
Thank you. :)

>why can't I have the files separate?
It sounds as if you don't have your project set up properly. What happens is that the header file contains declarations and the implementation files contain definitions. The header files are included in the implementation files only so that they can compile. Once the implementation files are compiled into object files, the object files are linked together into an executable.

If one of the implementation files isn't compiled into an object file then the linking step will fail if the other implementation files use functions or objects defined in that implementation file.

To recap, compilation requires that everything used be declared and linking requires that everything used be defined.

>what is the proper way to do it?
After starting Visual Studio, click on File, then New, and select Projet. From the New Project dialog, choose an Empty Project. This saves you from the irritating case where Visual Studio gives you precompiled headers (stdafx.h), and you can write code from scratch. Name the new project, and click OK.

Now you'll see the Solution Explorer with the project you just created. This is where you set everything up. Right click on Header Files and add either a new item or an existing item depending on whether or not you've already written the header files, and do the same thing for Source Files. This will get your project set up for building so that the linking step works properly. Type Ctrl+F5, click Yes, and the project should build and then run without any problems.

what is the proper way to do it?

After starting Visual Studio, click on File, then New, and select Projet. From the New Project dialog, choose an Empty Project. This saves you from the irritating case where Visual Studio gives you precompiled headers (stdafx.h), and you can write code from scratch. Name the new project, and click OK.

Now you'll see the Solution Explorer with the project you just created. This is where you set everything up. Right click on Header Files and add either a new item or an existing item depending on whether or not you've already written the header files, and do the same thing for Source Files. This will get your project set up for building so that the linking step works properly. Type Ctrl+F5, click Yes, and the project should build and then run without any problems.

I had tried that before. What I do is I make an empty project, then I make a header file that has the class definition. Then I make a cpp file that has the implementation. I put an #include in the cpp to include the header, and an #include in the main file also. I also put that #ifndef so it doesn't try to do it twice. but it still says unresolved external symbol. But when I cut everything from the cpp file and pasted it below the class definition in the .h file, IT WORKED. WHY?! I want them to be separate. I'm so confused...

Edited 3 Years Ago by Dani: Formatting fixed

Without being there to see what you're doing wrong, it's hard to say. But my first guess is that you've created the files, but failed to add them to the project correctly. The only reason for that linker error is that the compiler sees your declarations, but the linker can't find the definitions.

Without being there to see what you're doing wrong, it's hard to say. But my first guess is that you've created the files, but failed to add them to the project correctly. The only reason for that linker error is that the compiler sees your declarations, but the linker can't find the definitions.

okay, I finished that project (god knows how, but it ran...) and now I'm starting all over. I'm making a binaryHeap class. I made the binaryHeap.h file and I put both the class definition and the implementation right below. I compiled and it works OK. But if I cut the implementation and paste it into binaryHeap.cpp file, it doesn't work!! I made sure to include the header on the cpp file. I can post the files before and after, would that help to tell me what's wrong?

thanks again for your help.

>would that help to tell me what's wrong?
Possibly, though your project file would also be helpful in this case. If you can't attach it then feel free to email it to me (PM for the address).

>would that help to tell me what's wrong?
Possibly, though your project file would also be helpful in this case. If you can't attach it then feel free to email it to me (PM for the address).

:cry: now it's working... I'm even more confused that before. I guess I'll let it go for now until it creeps its ugly head out again. Thank you so much for your help and support

This article has been dead for over six months. Start a new discussion instead.