
December 19th, 2004, 01:09 AM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Help : Error while using template
Code:
main.obj : error LNK2001: unresolved external symbol "public: __thiscall hashtable<int>::~hashtable(void)" (??1?$hashtable@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall hashtable<int>::hashtable(int)" (??0?$hashtable@H@@QAE@H@Z)
Debug/hashtable.exe : fatal error LNK1120: 2 unresolved externals
That is the error I get when I compile this program. Program is hashtable using template. I used int instead of <class TYPE>, and it worked. then I switched to <class TYPE> and hell broke loose :-/
any clues to what i did wrong here?
main
Code:
#include "hashtable.h"
void main()
{
hashtable<int> T(10);
}
h
Code:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <iostream>
using namespace std;
template <class TYPE>
class hashtable
{
public:
hashtable( int );
hashtable( const hashtable<TYPE> & original );
~hashtable();
int Insert( TYPE );
bool Delete( TYPE );
int Search( TYPE );
void Print( ostream & );
private:
int hash( int );
TYPE * Table;
bool * filled;
int maxsize;
};
#endif
cpp
Code:
#include <iostream>
using namespace std;
#include "hashtable.h"
template <class TYPE>
hashtable<TYPE>::hashtable(int size)
{
// set maxsize
maxsize = size;
// create dynamic spaces
Table = new TYPE [maxsize];
filled = new bool [maxsize];
// initialize filled to false
for (int i=0; i<maxsize; i++) filled[i]=false;
}
template <class TYPE>
hashtable<TYPE>::hashtable(const hashtable<TYPE> & original)
{
maxsize = original.maxsize;
Table = new TYPE [maxsize];
filled = new bool [maxsize];
for (int i=0; i<maxsize; i++)
{
Table[i]=original.Table[i];
filled[i]=original.filled[i];
}
}
template <class TYPE>
hashtable<TYPE>::~hashtable()
{
// if dynamic storage exists delete it
if (Table)
{
delete [] Table;
delete [] filled;
}
// ... and set their pointers to 0
Table=0;
filled=0;
}
template <class TYPE>
int hashtable<TYPE>::Insert(TYPE item)
{
int hashed = hash(item);
if (!filled[hashed])
{
// no collision then.. input
Table[hashed] = item;
filled[hashed] = true;
// return the location of input
return hashed;
}
// return -1 when collision occured
return -1;
}
template <class TYPE>
bool hashtable<TYPE>::Delete(TYPE item)
{
int hashed = hash(item);
// if found at the address, set filled to false.
if (filled[hashed] && Table[hashed]==item)
{
filled[hashed] = false;
return true;
}
return false;
}
template <class TYPE>
int hashtable<TYPE>::Search(TYPE item)
{
int hashed = hash(item);
if (filled[hashed] && Table[hashed]==item)
return hashed; // found, returning position
else return -1; // not found
}
template <class TYPE>
void hashtable<TYPE>::Print(ostream &out)
{
for (int i=0; i<maxsize; i++)
{
// if filled, output as needed
if (filled[i]) out << "Table " << i << " - " << Table[i] << endl;
}
}
template <class TYPE>
int hashtable<TYPE>::hash(int/*TYPE*/ item)
{
const int MULTIPLIER = 25173;
const int ADDEND =13849;
int MODULUS = maxsize;
return ((MULTIPLIER * static_cast<int>(item)) + ADDEND) % MODULUS;
}
|