Hey all,
I'm implementing a hash table for a spell checker and need some assistance on vector const_iterator - at least I think that's where the problem lies. Can anyone assist in diagnosing how to fix these compiler errors?
I initially got this error, which i think will come back once I fix the is_present function:
main.cpp:101: undefined reference to `ns_1::table<word_record>::is_present(int) const'
I am now getting the below errors and can't figure out if I have a data type issue or am just defining the const_iterator incorrectly or ... Note that line 67 in the errors corresponds to line 11 of the template on the posting.
In file included from table3.h:82,
from main.cpp:10:
table3.template: In member function `bool ns_1::table<RecordType>::is_present(int) const':
table3.template:67: error: expected `;' before "it"
table3.template:69: error: `it' was not declared in this scope
table3.template: In member function `bool ns_1::table<RecordType>::is_present(int) const [with RecordType = word_record]':
main.cpp:102: instantiated from here
table3.template:67: error: dependent-name ` std::vector<RecordType,std::allocator<_CharT> >::const_iterator' is parsed as a non-type, but instantiation yields a type
table3.template:67: note: say `typename std::vector<RecordType,std::allocator<_CharT> >::const_iterator' if a type is meant
make: *** [main.o] Error 1
Note - I am already using other functions fromt the namespace ns_1 successfully.
// Header code
#include <cstdlib> // provides size_t
#include <vector> // provides STL vector class
namespace ns_1
{
template <class RecordType>
class table
{
public:
// MEMBER CONSTANT
static const std::size_t TABLE_SIZE = 1373;
// CONSTRUCTORS (no desctructor needed, uses automatic copy constructor)
table( );
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
// CONSTANT MEMBER FUNCTIONS
void find(int key, bool& found, RecordType& result) const;
bool is_present(int key) const;
std::size_t size( ) const { return total_records; }
private:
std::vector<RecordType> data[TABLE_SIZE]; // each component of the array is a vector
std::size_t total_records;
// HELPER MEMBER FUNCTION
std::size_t hash(int key) const;
};
}
#include "table3.template" // Include the implementation
// applicable template code
#include <cstdlib> // Provides size_t
#include <vector> // Provides vector class
namespace ns_1
{
template <class RecordType>
bool table<RecordType>::is_present(int key) const
{
std::size_t i = hash(key); // If key exists, it will be somewhere in data[i]
std::vector<RecordType>::const_iterator it;
for (it = data[i].begin(); it != data[i].end(); ++i)
{
if (data[i].at(*it.key) == key)
return true;
}
return false;
}
}
// code from main.cpp
// FILE: main.cpp
#include <iostream> // provides cin, cout,...
#include <sstream> // provides stringstream class and functionsl
#include <string> // provides string manipulation
#include <fstream> // provides file handling
#include <cassert> // provides assert
#include <cstdlib> // provides NULL constant
#include <vector> // provides vector class
#include "table3.h" // provides hash table class
using namespace std;
using namespace ns_1;
struct word_record
{
int key;
string data;
};
class HFstring // Hash Function for a string
{
public:
unsigned int operator() (const string& item) const
{
unsigned int prime = 2049982463;
int n = 0, i;
for (i = 0; i < item.length(); ++i)
n = n*8 + item[i];
//cout << " N equals " << n << endl;
return n > 0 ? (n % prime) : (-n % prime);
}
};
ifstream& getWord(ifstream& fin, string& w)
{
char c;
w = ""; // clear the string of characters
while (fin.get(c) && !isalpha(c)); // do nothing. just ignore c
// return on end-of-file
if (fin.eof())
return fin;
// record first letter of the word
w += tolower(c);
// collect letters and digits
while (fin.get(c) && (isalpha(c) || isdigit(c)))
w += tolower(c);
return fin;
}
int main ()
{
ifstream user_file;
ifstream dictionary;
table<word_record> d_table;
bool found;
word_record temp_record;
word_record d_entry;
HFstring genkey;
string docname;
string check_word;
string word;
int words_added;
dictionary.open("dict.dat");
assert(dictionary);
// Create dictionary hash table for spell check
while (dictionary) {
while (getline(dictionary, word)) {
// Insert data into a word_record object
d_entry.data = word;
d_entry.key = genkey(word);
d_table.insert(d_entry);
//++words_added;
//cout << "Inserted word number : " << words_added << endl;
}
cout << "Hash table successfully created" << endl;
cout << "Total Records equals" << d_table.size() << endl;
}
// Open user file to spell check
cout << "Please specify the filename to spell check: ";
cin >> docname;
user_file.open(docname.c_str());
assert(user_file);
//
while (user_file) {
while (getWord(user_file, check_word)) {
temp_record.data = check_word;
temp_record.key = genkey(check_word);
if (!(d_table.is_present(temp_record.key)))
cout << temp_record.data << " is not spelled correctly." << endl;
}
}
dictionary.close();
user_file.close();
return 0;
}