C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 19th, 2004, 01:09 AM
fifidong fifidong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 1 fifidong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
   }
   

Reply With Quote
  #2  
Old December 19th, 2004, 08:23 AM
marmo marmo is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 37 marmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 20 sec
Reputation Power: 5
Its a bug with MS IDE. For some reason it doesn't allow you to put templated function declarations and the definition in seperate files. The only thing I know of that you can do is to write the definition where you would normally put your declaration, or place the definition just below your class declaration inside the "hashtable.h" file.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help : Error while using template


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT