I've got a hasing template function that is giving me the error '!=' : no operator found which takes a left-hand operand of type 'StateData' (or there is no acceptable conversion). I have a StateData class that I've overloaded the != operator once as a friend and another time as a member of the class. Still, I get the same error. I'm at a loss as to why this is happening. Please help!
StateData
StateData functions
hashT class
insert function - where error occurs
here is the main
StateData
Code:
class StateData
{
public:
StateData();
StateData(string, string, int, int);
void setStateInfo(string, string, int, int);
string getName();
string getCapitol();
int getYearAdmit();
int getOrder();
StateData getStateData();
friend bool operator<(StateData &, StateData &);
friend bool operator>(StateData &, StateData &);
// friend bool operator!=(StateData &, StateData &);
bool operator!=(StateData &);
friend bool operator==(StateData &, StateData &);
friend ostream &operator <<(ostream&, StateData);
friend istream &operator>>(istream &, StateData &);
private:
string name;
string capitol;
int yearAdmit;
int order;
};
Code:
StateData::StateData()
{
name = " ";
capitol = " ";
yearAdmit = 0;
order = 0;
}
StateData::StateData(string n, string c, int y, int o)
{
name = n;
capitol = c;
yearAdmit = y;
order = o;
}
void StateData::setStateInfo(string n, string c, int y, int o)
{
name = n;
capitol = c;
yearAdmit = y;
order = o;
}
string StateData::getName()
{ return name; }
string StateData::getCapitol()
{ return capitol; }
int StateData::getYearAdmit()
{ return yearAdmit; }
int StateData::getOrder()
{ return order; }
//StateData StateData::getStateData()
// { return this; }
bool operator<(StateData &right, StateData &left)
{ return (right.getName() < left.getName()); }
bool operator>(StateData &right, StateData &left)
{ return (right.getName() > left.getName()); }
bool operator==(StateData &right, StateData &left)
{ return (right.getName() == left.getName()); }
//bool operator!=(StateData &right, StateData &left)
//{ return (right.getName() != left.getName()); }
bool StateData::operator!=(StateData &left)
{ return (this->name != left.name); }
ostream &operator <<(ostream& out, StateData &a)
{
out << "State name: " << a.getName() << endl;
out << "State capitol: " << a.getCapitol() << endl;
out << "Year of admittance: " << a.getYearAdmit() << endl;
out << "Order of admittance: " << a.getOrder() << endl;
return out;
}
istream &operator>>(istream &in, StateData &a)
{
string n, c;
int y, o;
cout << "Enter State name: ";
getline(cin, n);
cout << "Enter State capitol: ";
getline(cin, c);
cout << "Enter year of admittance: ";
cin >> y;
cout << "Enter order of admittance: ";
cin >> o;
a.setStateInfo(n, c, y, o);
return in;
}
Code:
template<class elemType>
class hashT
{
private:
elemType *HTable; //pointer to the hash table
int *indexStatusList; //pointer to the array indicating
//the status of a position in the
//hash table
int length; //number of items in the hash table
int HTSize; //maximum size of the hash table
};
Code:
template <class elemType>
void hashT<elemType>::insert(int hashIndex, const elemType& rec)
{
int pCount;
int inc;
pCount = 0;
inc = 1;
while(indexStatusList[hashIndex] == 1
&& HTable[hashIndex] != rec
&& pCount < HTSize / 2)
{
cout <<"inc = " << inc << endl;
pCount++;
hashIndex = (hashIndex + inc ) % HTSize;
cout << "new hashIndex = " << hashIndex << endl;
inc = inc + 2;
}
if(indexStatusList[hashIndex] != 1)
{
HTable[hashIndex] = rec;
cout << "HTable["<< hashIndex <<"]" <<" = " << rec << endl;
indexStatusList[hashIndex] = 1;
length++;
}
else
if(HTable[hashIndex] == rec)
cerr<<"Error: No duplicates are allowed"<<endl;
else
cerr<<"Error: The table is full. "
<<"Unable to resolve the collision"<<endl;
}
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "hashT.h"
#include "stateData.h"
using namespace std;
int hashFunc(string name, int size);
int main()
{
hashT<StateData> HashTable(50);
int size = 15;
ifstream infile;
infile.open("states.txt");
if(!infile)
{
cout << "Error reading states file.\n";
exit(1);
}
char ch;
int year, order, key;
string state, capitol;
bool found;
getline(infile, state);
while(infile)
{
getline(infile, capitol);
infile >> year;
infile >> order;
StateData temp;
temp.setStateInfo(state, capitol, year, order);
infile.get(ch);
key = hashFunc(state, size);
HashTable.insert(key, temp);
getline(infile, state);
}
HashTable.print();
//cout<<"Enter item to be deleted: ";
//cin>>num;
//cout<<endl;
infile.close();
system("pause");
return 0;
}
int hashFunc(string name, int size)
{
int i, sum, len;
i= 0;
sum = 0;
len = name.length();
for (int k=0; k < 15 - len; k++)
name = name + ' ';
for (int k = 0; k < 5; k++)
{
sum = sum + static_cast<int>(name[i]) * 128 * 128
+ static_cast<int>(name[i+1]) * 128
+ static_cast<int>(name[i+2]);
i = i+3;
}
return sum % size;
}