| | |
Constructor Error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 0
My compiler is throwing me this error when I try to make a new one.
I am trying to make it with this
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
I am trying to make it with this
C++ Syntax (Toggle Plain Text)
string a = "five"; 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)
// #include "hashTable.h" #include <iostream> #include <list> #include <string> #include <math.h> #include <iterator> bool checkforlargeprime (int num); bool checkprime (int nn) { if (nn > 100) { return checkforlargeprime(nn); } else { int k=2; while (k < nn) { int sd = nn%k; if ( sd == 0) { return false; } k++; } } return true; } int getNextPrimeNumber (int num) { int nam = num+1; bool das = true; while ( das == true ) { if (checkprime(nam)) das = false; else nam = nam+1; } return nam; } bool checkforlargeprime (int num) { if (num > 100) { int sss = ((int)(sqrt((double)num)))+1; int pn = 2; while (pn < sss) { if (num%pn == 0) { return false; } pn = getNextPrimeNumber(pn); } return true; } else { return false; } } template <class HashedObj> HashTable<HashedObj>::HashTable( const HashedObj & notFound, int size ) : item( notFound ), theLists( getNextPrimeNumber( size ) ) { } template <class HashedObj> void HashTable<HashedObj>::insert( const HashedObj & x ) { list<HashedObj> &whichList = theLists[hash(x, theLists.size())]; typename std::list<HashedObj>::iterator itr = whichList.find(x); if( itr.isPastEnd( ) ) whichList.insert( x, whichList.zeroth( ) ); } template <class HashedObj> void HashTable<HashedObj>::remove(const HashedObj &x ) { theLists[hash( x, theLists.size( ))].remove(x); } template <class HashedObj> bool HashTable<HashedObj>::contains(const HashedObj &x )const { const list<HashedObj> & whichList = theLists[hash(x)]; return find(whichList.begin(),whichList.end(),x)!=whichList.end(); } template <class HashedObj> const HashedObj &HashTable<HashedObj>::find( const HashedObj &x ) const { typename std::list<HashedObj>::iterator itr; itr = theLists[ hash( x, theLists.size( ) ) ].find( x ); if( itr.isPastEnd( ) ) return item; else return itr.retrieve( ); } template <class HashedObj> void HashTable<HashedObj>::makeEmpty( ) { for( int i = 0; i < theLists.size( ); i++ ) theLists[ i ].makeEmpty( ); } int hash( const string &key, int tableSize) { int hashVal = 0; for(int i = 0; i<key.length(); i++) hashVal = 37*hashVal+key[i]; hashVal %= tableSize; if(hashVal<0) hashVal += tableSize; return hashVal; }
C++ Syntax (Toggle Plain Text)
#ifndef HASHTABLE_H #define HASHTABLE_H #include <iostream> #include <vector> #include <list> using namespace std; template <typename HashedObj> class HashTable { public: explicit HashTable( const HashedObj & notFound, int size = 101 ); HashTable( const HashTable & rhs ): item( rhs.item ), theLists( rhs.theLists ) { } void makeEmpty(); void insert(const HashedObj &x); void remove(const HashedObj &x); bool contains(const HashedObj &x)const; const HashedObj & find( const HashedObj & x ) const; private: const HashedObj item; vector<list<HashedObj> > theLists; }; template <typename HashedObj> int hash(const HashedObj &x, int tablesize); #endif
0
#2 Oct 18th, 2009
You need to put the definition and the declaration of a template class
in the same file.
in the same file.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
Join Date: Nov 2008
Posts: 393
Reputation:
Solved Threads: 72
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
Then you don't have to change the includes in anyway, and you don't have a huge .h file to include each time.
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
![]() |
Similar Threads
- Constructor error (C++)
- no appropriate default constructor available error (C++)
- Receiving Error with a Set Function (C++)
- Help me with this run time error (C++)
- Constructor Error (C++)
Other Threads in the C++ Forum
- Previous Thread: reading a bitmap?
- Next Thread: Convert struct from native to managed code
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






