Reputation: 25
I would like to get some help with my current project as I struggle to understand what exactly went wrong in my program. I believe the problem is with my constructor. When I call member function it behaves as I haven't initialized my class members.
Here is my class:
class BankList {
public:
// constructor
BankList();
BankList(int size);
bool isEmpty() const;
int size() const;
void insertBankEntry(std::string account, std::string level, std::string lName, std::string fName, float value); // add a single entry to the list
void insertBankData(std::string fileName); // populate list data from the file
void deleteBankEntry(std::string key); // delete a single entry
void findBankEntry(std::string key) const; // Find and display one element using key
void checkBankEntry(std::string account);
void printHashBankData() const; // List data in hash table sequence
void printHashKeyBankData() const; // List data in key sequence (sorted)
void printTreeBankData() const; // Print indented tree
void writeBankData(); // Write data to a file
void outputHashStatistic() const; // print hash stats
private:
HashList* hashlist;
Tree* tree;
int count; // number of records
int hashSize;
};
Here are my constructors:
BankList::BankList()
{
HashList* hashlist = new HashList();
Tree* tree = new Tree();
count = 0;
hashSize = 0;
}
BankList::BankList(int size)
{
HashList* hashlist = new HashList(size);
Tree* tree = new Tree();
count = 0;
hashSize = size;
}
The function I am trying to call:
void BankList::insertBankEntry(string account, string level, string lName, string fName, float value) // add a single entry to the list
{
BankCustomer* customer = new BankCustomer(account, level, lName, fName, value);
hashlist->insert(customer);
tree->Insert(customer);
count++;
}
However, it does work if I place this code in my function.
if (!tree || !hashlist)
{
tree = new Tree();
hashlist = new HashList();
}
main:
int size = getSize();
BankList* list = new BankList(size);
list->insertBankEntry("123412341234", "Gold", "Jonhson", "Steve", 1234.45);
Thanks in advance!
Upvotes: 0
Views: 589
Reputation: 56557
In the constructors you are hiding the member variables (by declaring variables with the same name as the members) hence your member variables remain un-initialized
HashList* hashlist = new HashList(); // hiding the member variable this->hashlist
Tree* tree = new Tree(); // hiding the member variable this->tree
Just use
hashlist = new HashList();
tree = new Tree();
inside the constructors.
Upvotes: 2