944,081 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 344
  • C++ RSS
Oct 18th, 2009
0

Constructor Error

Expand Post »
My compiler is throwing me this error when I try to make a new one.
I am trying to make it with this

C++ Syntax (Toggle Plain Text)
  1. string a = "five";
  2. HashTable<string> *hashy = new HashTable<string>(a,100);

This is the compiler error
(.text+0x1d3): undefined reference to `HashTable<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::HashTable(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: ld returned 1 exit status



C++ Syntax (Toggle Plain Text)
  1. //
  2. #include "hashTable.h"
  3. #include <iostream>
  4. #include <list>
  5. #include <string>
  6. #include <math.h>
  7. #include <iterator>
  8. bool checkforlargeprime (int num);
  9.  
  10. bool checkprime (int nn) {
  11. if (nn > 100) {
  12. return checkforlargeprime(nn);
  13. } else {
  14. int k=2;
  15. while (k < nn) {
  16. int sd = nn%k;
  17. if ( sd == 0) {
  18. return false;
  19. }
  20. k++;
  21. }
  22. }
  23. return true;
  24. }
  25. int getNextPrimeNumber (int num) {
  26. int nam = num+1;
  27. bool das = true;
  28. while ( das == true ) {
  29. if (checkprime(nam))
  30. das = false;
  31. else
  32. nam = nam+1;
  33. }
  34. return nam;
  35. }
  36. bool checkforlargeprime (int num) {
  37. if (num > 100) {
  38. int sss = ((int)(sqrt((double)num)))+1;
  39. int pn = 2;
  40.  
  41. while (pn < sss) {
  42. if (num%pn == 0) {
  43. return false;
  44. }
  45. pn = getNextPrimeNumber(pn);
  46. }
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52.  
  53. template <class HashedObj>
  54. HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size )
  55. : item( notFound ), theLists( getNextPrimeNumber( size ) )
  56. {
  57. }
  58. template <class HashedObj>
  59. void HashTable<HashedObj>::insert( const HashedObj & x )
  60. {
  61. list<HashedObj> &whichList = theLists[hash(x, theLists.size())];
  62. typename std::list<HashedObj>::iterator itr = whichList.find(x);
  63.  
  64. if( itr.isPastEnd( ) )
  65. whichList.insert( x, whichList.zeroth( ) );
  66. }
  67. template <class HashedObj>
  68. void HashTable<HashedObj>::remove(const HashedObj &x )
  69. {
  70. theLists[hash( x, theLists.size( ))].remove(x);
  71. }
  72. template <class HashedObj>
  73. bool HashTable<HashedObj>::contains(const HashedObj &x )const
  74. {
  75. const list<HashedObj> & whichList = theLists[hash(x)];
  76. return find(whichList.begin(),whichList.end(),x)!=whichList.end();
  77. }
  78.  
  79. template <class HashedObj>
  80. const HashedObj &HashTable<HashedObj>::find( const HashedObj &x ) const
  81. {
  82. typename std::list<HashedObj>::iterator itr;
  83. itr = theLists[ hash( x, theLists.size( ) ) ].find( x );
  84. if( itr.isPastEnd( ) )
  85. return item;
  86. else
  87. return itr.retrieve( );
  88. }
  89. template <class HashedObj>
  90. void HashTable<HashedObj>::makeEmpty( )
  91. {
  92. for( int i = 0; i < theLists.size( ); i++ )
  93. theLists[ i ].makeEmpty( );
  94. }
  95.  
  96. int hash( const string &key, int tableSize)
  97. {
  98. int hashVal = 0;
  99. for(int i = 0; i<key.length(); i++)
  100. hashVal = 37*hashVal+key[i];
  101. hashVal %= tableSize;
  102. if(hashVal<0)
  103. hashVal += tableSize;
  104. return hashVal;
  105. }

C++ Syntax (Toggle Plain Text)
  1. #ifndef HASHTABLE_H
  2. #define HASHTABLE_H
  3. #include <iostream>
  4. #include <vector>
  5. #include <list>
  6. using namespace std;
  7. template <typename HashedObj>
  8. class HashTable
  9. {
  10. public:
  11. explicit HashTable( const HashedObj & notFound, int size = 101 );
  12. HashTable( const HashTable & rhs ): item( rhs.item ), theLists( rhs.theLists ) { }
  13. void makeEmpty();
  14. void insert(const HashedObj &x);
  15. void remove(const HashedObj &x);
  16. bool contains(const HashedObj &x)const;
  17. const HashedObj & find( const HashedObj & x ) const;
  18. private:
  19. const HashedObj item;
  20. vector<list<HashedObj> > theLists;
  21. };
  22. template <typename HashedObj>
  23. int hash(const HashedObj &x, int tablesize);
  24. #endif
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
killerqb is offline Offline
20 posts
since Sep 2009
Oct 18th, 2009
0
Re: Constructor Error
You need to put the definition and the declaration of a template class
in the same file.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 18th, 2009
0
Re: Constructor Error
firstPerson's answer works and solves the problem BUT for a large classes you might prefer to use explicit instantiation. [which gives the beginner a better idea of what is happening (sometimes)].

so you add a
template class HashTable<std::string>; at the end of HashTable.cpp and then you have told the compiler that you require a particular HashTable type.

Then you don't have to change the includes in anyway, and you don't have a huge .h file to include each time.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: reading a bitmap?
Next Thread in C++ Forum Timeline: Convert struct from native to managed code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC