class - C++ program crashing (has something to do with with dynamic memory) -
i'm having weird crashing occurs everytime goes loop initialize each position undefined. can shine light on why happening? #include #include #include using namespace std;
class myphonebook { public: myphonebook(int, string, string, string, string); myphonebook(); ~myphonebook(); void initialise(int, int, string, string, string, string); bool search(string&, int); bool find_free_pos(); void add(); void remove(); void display(int); friend istream& operator >> (istream& in, myphonebook& ph); friend ostream& operator << (ostream& out, myphonebook& ph); private: int *recordid; int *status; // -1 no longer @ number, 0 blocked, 1 not blocked, 2 free string *name, *areacode, *number, *group; }; int main(int argc, char** argv) { cout << "test 1" << endl; myphonebook *myphbk; // pointer point object of myphonebook class myphbk = new myphonebook[100]; // using dynamic memory cout << "test 2" << endl; //just testing int pos = 0; for(pos = 0; pos < 100; pos++) // initializing undefined, , position free (position second parameter sended in) { myphbk[pos].initialise( (pos+1) , 2 , "undefined" , "undefined" , "undefined" , "undefined"); } cout << "test 3" << endl; //just testing } return 0; void myphonebook::initialise(int recordid_,int status_, string name_, string areacode_, string number_, string group_) { //now assign them private member variables *recordid = recordid_; *status = status_; *name = name_; *areacode = areacode_; *number = number_; *group = group_; //end of assigning }
does have idea why can't program reach cout << "test 3" << endl part of program without crashing?
since did not paste constructor of myphonebook
can guess, problem lines
*recordid = recordid_; *status = status_;
if did not assign valid address recordid
, status
in constructor, e.g. by
recordid = new int; status = new int;
you might want declare member variables simple int
.
Comments
Post a Comment