I’m trying to write a program, but I can’t figure out why my private member function can’t access my private data members. Will somebody please help? Here’s my function. nStocks, capacity, and slots[] are all private data members, and hashStr() is a private function.
bool search(char * symbol)
{
if (nStocks == 0)
return false;
int chain = 1;
bool found = false;
unsigned int index = hashStr(symbol) % capacity;
if (strcmp(symbol, slots[index].slotStock.symbol) != 0)
{
int start = index;
index ++;
index = index % capacity;
while (!found && start != index)
{
if(symbol == slots[index].slotStock.symbol)
{
found = true;
}
else
{
index = index % capacity;
index++;
chain++;
}
}
if (start == index)
return false;
}
return true;
}
Here is the private members section of my .h file:
private:
static unsigned int hashStr(char const * const symbol); // hashing function
bool search(char * symbol);
struct Slot
{
bool occupied;
Stock slotStock;
};
Slot *slots; // array of instances of slot
int capacity; // number of slots in array
int nStocks; // current number of stocks stored in hash table
Please let me know if I can provide any additional information.
Your code creates a non-member function called
search. You need to change:To:
Replace
ClassNamewith the name of the class.