I tried to initialize the the Employer object using the emp2 pointer but I keep getting an error :
Unhandled exception at 0x008d2c6b in empPract.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
I tried finding the error by stepping through the code and looking up the error code online. How can I identify and correct the problem?
Here's the code:
Main Driver:
#include "Employer.h"
#include <string>
#include <iostream>
using namespace std;
void main(){
Employer *emp2;
emp2=new Employer();// this is where i tried to initialize the employer
cout <<"Employer id:" << emp2->getUniqueId() << endl;
system("PAUSE");
}// end main
Employer class:
#ifndef EMPLOYER_H
#define EMPLOYER_H
#include <time.h>
#include <iostream>
#include "JobSeeker.h"
#include <string>
#include "Address.h"
//#define MAX 1000;
using namespace std;
class Employer{
private:
int empID;
string companyName; // addresss of the company
Address *empAddress;
string compTelephone;
string areaOfEmployment;
//Person *manager;
string contactName;
string contactTitle;
int maxRecruits; // number of employees needed
int totalRecruited;
int totalPlaced;
// links
Employer *next;
public:
Employer(){
empID=0;
companyName=" ";
compTelephone=" ";
areaOfEmployment=" ";
contactName=" ";
contactTitle=" ";
maxRecruits=0;
totalRecruited=0;
totalPlaced=0;
empAddress->setStreet(" ");
empAddress->setStreetNum(" ");
empAddress->setParish(" ");
// set links
next=NULL;
}// default constructor
Employer(int id, string cname, Address *usrAdd, string telephone, string nameOfContact, string contTitle, string aoe ){
empID=id;
companyName=cname;
compTelephone= telephone;
contactName=nameOfContact;
contactTitle=contTitle;
areaOfEmployment=aoe;
empAddress->setStreet(usrAdd->getStreet());
empAddress->setStreetNum(usrAdd->getStreetNum());
empAddress->setParish(usrAdd->getParish());
// set links
next=NULL;
}// Primary Constructor */
//copy constructor
Employer(Employer *emp1){
empID=emp1->getUniqueId();
companyName=emp1->getCompanyName();
compTelephone=emp1->getCompTelephone();
contactName=emp1->getContactName();
contactTitle=emp1->getContactTitle();
areaOfEmployment=emp1->getAOE();
empAddress=emp1->getAddress();
next=NULL;
}// copy constructor
//destructor
Employer::~Employer(){
delete empAddress;
delete next;
}// destructor
void setEmpID(){
// srand(5);
// EmpID = rand()%MAX+1;
}// set id
int getUniqueId(){
return empID;
}// end of getter for EmpID
void setCompanyName(string cname){
companyName=cname;
}// end of setter for company name
string getCompanyName(){
return companyName;
}// end of function to return companyName
void setCompTelephone(string cTele){
compTelephone=cTele;
}// end of setter for company's telephone number
string getCompTelephone(){
return compTelephone;
}// end of getter for company telephone number
void setContactName(string cName){
contactName=cName;
}// end of setContactName
string getContactName(){
return contactName;
}// end of getter for contactName
void setContactTitle(string title){
contactTitle=title;
}// end of setter for contactTitle
string getContactTitle(){
return contactTitle;
}// end of getter for contactTitle
void setMaxRecruits(int max){
maxRecruits=max;
} // end of setter for maxRecruits
int getMaxRecruits(){
return maxRecruits;
}// end of getter for maxRecruits
void setAreaOfEmployment( string aoe){
areaOfEmployment=aoe;
}// end of setter for aoe
string getAOE(){
return areaOfEmployment;
}// end of getter for areaOfEmployment
// set links
void setNext(Employer *nx){
next=nx;
}// end mutator for setNexEmp
void setAddress(string street, string streetNumber, string par){
empAddress->setStreet(street);
empAddress->setStreetNum(streetNumber);
empAddress->setParish(par);
}// end of setter for employer address
Address * getAddress(){
return empAddress;
}// end of getter for address
// set links
Employer *getNext(){
return next;
}// end of getter for nextEmployer
};
#endif
JobSeeker class:
#ifndef JOBSEEKER_H
#define JOBSEEKER_H
#include <string>
#include "Address.h"
#include "Person.h"
#include <time.h>
#include "Date.h"
using namespace std;
class JobSeeker:public Person{
private:
string middlename;
string gender;
string TRN;
string NIS;
Person *reference[2];
Date *dob;
string AreaOfInt;
string recruitmentStatus;
string companyPlaced;
time_t lastSelectedDate; // date the JobSeeker was last recruited/ placed
time_t dateOfRegistration; // date the job seekeer registered
Address *jSeekerAddress;
// links
JobSeeker *next;
public:
JobSeeker(){
int i;
next=NULL;
dob=NULL;
middlename="";
firstname="";
lastname="";
TRN="";
NIS="";
gender="";
wrkNum="";
cellNum="";
homeNum="";
for (i=0; i<2;i++){
reference[i]->setFirstName("");
reference[i]->setLastname("");
reference[i]->setCellNum("");
reference[i]->setHomeNum("");
reference[i]->setWrkNum("");
Address *temp=new Address();
reference[i]->setAddress(temp);
}// end for loop
AreaOfInt="";
recruitmentStatus="";
companyPlaced="";
lastSelectedDate=time(0); // last selected date for job seeker
dateOfRegistration=time(0); // date the job seeker registered
jSeekerAddress->setStreet("");
jSeekerAddress->setStreetNum("");
jSeekerAddress->setParish("");
}// end of default constructor for job seeker
JobSeeker(JobSeeker *js){
int i;
firstname= js->getFirstname();
middlename=js->getMiddlename();
lastname=js->getLastname();
dob=js->getDOB();
TRN=js->getUniqueId();
NIS=js->getNIS();
gender=js->getGender();
wrkNum=js->getWrkNum();
cellNum= js->getCellNum();
homeNum=js->getHomeNum();
reference[i]= js->getReference1();
reference[i]=js->getReference2();
//reference[i]->
lastSelectedDate=js->getSelectedDate();
dateOfRegistration=js->getDateOFReg();
AreaOfInt=js->getAreaOFInt();
recruitmentStatus=js->getRecruitStatus();
companyPlaced=js->getCompanyPlaced();
// set link to next jobseeker
next=js->getNext();
}// copy constructor
// destructor
JobSeeker::~JobSeeker(){
delete next;
delete dob;
delete reference[0]; reference[1];
delete jSeekerAddress;
}// destructor
void setMiddlename(string mname){
middlename=mname;
}// end of setter for middlename
string getMiddlename(){
return middlename;
}// end of getter for middlename
void setTRN(string usrTRN){
TRN=usrTRN;
}// end setter for TRN
string getUniqueId(){
return TRN;
}// end of getter for TRN
void setNIS(string usrNIS){
NIS=usrNIS;
}// end setter for NIS
string getNIS(){
return NIS;
}// end of getter for NIS
void setGender(char g){
gender = g;
}// end of gender setter
string getGender(){
return gender;
}// end of getter for gender
void setAreaOFInterest(string aoi){
AreaOfInt=aoi;
}// end of setter for AreaOfInt
string getAreaOFInt(){
return AreaOfInt;
}// end of getter for AreaOfInt
void setDOB(int d, int m, int y){
dob->setDay(d);
dob->setbMonth(m);
dob->setYear(y);
}// setter for date of birth of person
Date* getDOB(){
return dob;
}// end of getter for DOB
void addReference(Person *p, int numOfRef){
int count=0;
while(count <= numOfRef){
reference[count]->setFirstName(p->getFirstname());
reference[count]->setLastname(p->getLastname());
reference[count]->setCellNum(p->getCellNum());
reference[count]->setWrkNum(p->getWrkNum());
reference[count]->setHomeNum(p->getHomeNum());
//Address ad= p.getAddress();
reference[count]->setAddress(p->getStreetName() ,p->getStreetNum(),p->getParish());
//reference[count]->setAddress(p->getAddress());
}
}// end of loop to add reference
Person* getReference1(){
return reference[0];
}// end of getter for reference1
Person* getReference2(){
return reference[1];
}// end of getter for reference2
// method to set the registration date
void setDateOFReg(){
dateOfRegistration=time(0); // sets the date to the computer's date
}// end of method to set registration date
void setCompanyPlaced(string company){
companyPlaced=company;
}// end of setter for companyPlaced
string getCompanyPlaced(){
return companyPlaced;
}// end of accessor for comp placed
void setRecruitStatus(string status){
recruitmentStatus=status;
}// setter for recruitment status
string getRecruitStatus(){
return recruitmentStatus;
}// end of accessor for recruitment status
// method to get the registration date
time_t getDateOFReg(){
return dateOfRegistration;
/* Hubert please remember that the date is not formatted. to do so:
you must use 'ctime(&variableName)' with cout or gui
and remembe to include 'time.h' */
}// end of getter for registration date
void setDateSelected(){
lastSelectedDate=time(0); // this sets the time to the cuurrent date
}// sets last selected date to current date
time_t getSelectedDate(){
return lastSelectedDate; // remember needs formatting like registration date too
}// returns the last selected date
// set links (pointer)
void setNext(JobSeeker *n){
next=n;
}// end of setNext
JobSeeker * getNext(){
return next;
}// end of getNext
};// end class
#endif // end of Job Seeker class
Person class:
#ifndef PERSON_H
#define PERSON_H
#include "Address.h"
#include <string>
using namespace std;
class Person{
protected:
string firstname;
string lastname;
Address *personAddress;
string wrkNum;
string cellNum;
string homeNum;
//Person *persPointer;
public:
Person(){
firstname= "default";
lastname="default";
wrkNum="";
cellNum="";
homeNum="";
personAddress->setStreet("");
personAddress->setStreetNum(0);
personAddress->setParish("blank");
}// default constructor for person
//Address *personAddress;
void setFirstName(string fname){
firstname=fname;
}// end of firstname setter
void setLastname(string lname){
lastname=lname;
}// end of lastname setter
string getFirstname(){
return firstname;
}// end of getter for firstname
string getLastname(){
return lastname;
}// end of getter for lastname
void setWrkNum(string tNum){
wrkNum=tNum;
}// end of method to set home number
string getWrkNum(){
return wrkNum;
}// end of getter for telNum
void setCellNum(string cNum){
cellNum=cNum;
}// end of setter for cellNum
string getCellNum(){
return cellNum;
}// end of getter for cellNum
void setHomeNum(string hNum){
homeNum=hNum;
}// setter for home number
string getHomeNum(){
return homeNum;
}// getter for homeNum
void setAddress(string streetName, string streetNum, string parish){
personAddress->setStreet(streetName);
personAddress->setStreetNum(streetNum);
personAddress->setParish(parish);
}// end of address setter
void setAddress(Address *ad){
personAddress->setStreet(ad->getStreet());
personAddress->setStreetNum(ad->getStreetNum());
personAddress->setParish(ad->getParish());
}
Address* getAddress(){
return personAddress;
}// get personAddress
string getStreetName(){
return personAddress->getStreet();
}// end of method to get Street name
string getStreetNum(){
return personAddress->getStreetNum();
}// end of method to get streetNum
string getParish(){
return personAddress->getParish(); ;
}// end of getter for parish
};// end of class Person
#endif // PERSON_H
Date class:
#ifndef Date_H
#define Date_H
#include <iostream>
using namespace std;
class Date{
private:
int day, month, year;
public:
Date(){
day= 0;
month=0;
year=0;
}// end of DOB default constructor
void setDay(int d){
day=d;
}// end of setter for bDay
// setbMonth
void setbMonth(int m){
month= m;
}// end of setter for bMonth
void setYear(int y){
year=y;
}// end of setter for year
// display method
void display(){
cout << month << "/ " << day <<"/ " << year << endl;
}// end diplay method for date
}; // emd of class DOB
#endif // Date_H
Address class:
// class used for the addresses of the employees and the job seekers
// Date created Feb 28, 2012
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
using namespace std;
class Address{
private:
string streetName;
string streetNum;
string parish;
public:
Address(){
streetName= "unknown";
streetNum="";
parish="blank";
}// end of default constructor for Address
// destructor for address
Address::~Address(){
}// end of destructor
void setStreet(string street){
streetName=street;
}// end of setter for street name
void setStreetNum(string streetNumber){
streetNum=streetNumber;
}// end of setter streetNumber
void setParish(string par){
parish=par;
}// end of setter for parish
//getters
string getStreet(){
return streetName;
}//end of getter for street
string getStreetNum(){
return streetNum;
}// end of getter for streetNum
string getParish(){
return parish;
}// end of getter for
};// end of class Address
#endif
For what i can see empAddress member in Employer class is a pointer and never initialised. In you employer contructor you try to access it
empAddress->setStreet(" ");
empAddress->setStreetNum(" ");
empAddress->setParish(" ");
Thus creating a memory violation error, because empAddress must contain garbage. A good pratice it to always set pointers to NULL, so if you crash you see a NULL value not some random value.
So create empAddress in contructor this->empAddress= new Address()
Another good pratice (matching the one i gave you): If your unused pointer or freed pointer are set to NULL, you can easily test them to see if you can use them or free them. For example in your employer destructor, you delete empAddress without any prior testing, that can cause error if you made a mistake somewhere else in you code. In your case if you didn't crash in contructor, you would have crashed in destructor.
You're setting empAddress->setStreet(" ");
in the default constructor, but empAddress is not initialized.
User contributions licensed under CC BY-SA 3.0