Constructor Error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 19
Reputation: killerqb is an unknown quantity at this point 
Solved Threads: 0
killerqb killerqb is offline Offline
Newbie Poster

Constructor Error

 
0
  #1
Oct 18th, 2009
My compiler is throwing me this error when I try to make a new one.
I am trying to make it with this

  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



  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. }

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,230
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 151
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #2
Oct 18th, 2009
You need to put the definition and the declaration of a template class
in the same file.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #3
Oct 18th, 2009
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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC