C++ Access violation reading location, class this pointer is NULL -
i current project struggle understand went wrong in program. believe problem constructor. when call member function behaves haven't initialized class members.
here 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 single entry list void insertbankdata(std::string filename); // populate list data file void deletebankentry(std::string key); // delete single entry void findbankentry(std::string key) const; // find , display 1 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 file void outputhashstatistic() const; // print hash stats private: hashlist* hashlist; tree* tree; int count; // number of records int hashsize; };
here 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 trying call:
void banklist::insertbankentry(string account, string level, string lname, string fname, float value) // add single entry list { bankcustomer* customer = new bankcustomer(account, level, lname, fname, value); hashlist->insert(customer); tree->insert(customer); count++; }
however, work if place code in 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!
in constructors hiding member variables (by declaring variables same name members) hence member variables remain un-initialized
hashlist* hashlist = new hashlist(); // hiding member variable this->hashlist tree* tree = new tree(); // hiding member variable this->tree
just use
hashlist = new hashlist(); tree = new tree();
inside constructors.
Comments
Post a Comment