| | |
unresolved external symbol when using a hashtable class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
here is the header for the hashtable class:
and here is the implementation for the hash table:
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!
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:
C++ Syntax (Toggle Plain Text)
// 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:
C++ Syntax (Toggle Plain Text)
#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 ¬Found, 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:
C++ Syntax (Toggle Plain Text)
#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 ¬Found, 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!
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
>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, 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?
I'm here to prove you wrong.
•
•
•
•
Originally Posted by Narue
>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?
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
>(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.
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.
I'm here to prove you wrong.
[QUOTE=Narue
>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.[/QUOTE]
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...
>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.[/QUOTE]
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...
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
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.
I'm here to prove you wrong.
•
•
•
•
Originally Posted by Narue
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.
thanks again for your help.
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
•
•
•
•
Originally Posted by Narue
>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).
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
![]() |
Similar Threads
- error LNK2001: unresolved external symbol - only happens with STATIC variable (C++)
- Unresolved external symbol when calling my class? (C++)
- unresolved external symbol? (C++)
- Unresolved External Symbol Error (C++)
- error LNK2001: unresolved external symbol (C++)
- error LNK2001: unresolved external symbol (C++)
Other Threads in the C++ Forum
- Previous Thread: Seg Fault from Matrix Data Constructor
- Next Thread: program for finding factorial of a number using *operator overloading
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






