Hi, I'm having an issue in my program when I try to call delete in my destructor.
Can someone take a look and see what I doing wrong? I've looked it over and over but I just can't figure out what's wrong.
destructor code:
StudentRecord::~StudentRecord(){ // Destructor
delete[] m_firstName;
delete[] m_lastName;
}
and this is my constructor code:
StudentRecord::StudentRecord(char* first,char* last,unsigned int id,float mark){ // Overloaded constructor
m_firstName = strcpy(new char[strlen(first)+1],first);
m_lastName = strcpy(new char[strlen(last)+1],last);
m_id = id;
m_mark = mark;
}
Here is the full code for my project in case it's needed.
StudentRecord.h
#ifndef STUDENTRECORD_H
#define STUDENTRECORD_H
class StudentRecord{
char* m_firstName;
char* m_lastName;
unsigned int m_id;
float m_mark;
public:
StudentRecord();
StudentRecord(char*,char*,unsigned int,float);
StudentRecord(StudentRecord&);
~StudentRecord();
StudentRecord& operator=(StudentRecord&);
bool operator>(StudentRecord&);
bool operator<(StudentRecord&);
void Report();
char* GetFirstName(){return m_firstName;}
char* GetLastName(){return m_lastName;}
unsigned int GetID(){return m_id;}
float GetMark(){return m_mark;}
};
#endif
StudentRecord.cpp
#include <iostream>
#include "StudentRecord.h"
using namespace std;
StudentRecord::StudentRecord(){ // Default constructor
m_firstName = NULL;
m_lastName = NULL;
m_id = 0;
m_mark = 0.00;
}
StudentRecord::StudentRecord(char* first,char* last,unsigned int id,float mark){ // Overloaded constructor
m_firstName = strcpy(new char[strlen(first)+1],first);
m_lastName = strcpy(new char[strlen(last)+1],last);
m_id = id;
m_mark = mark;
}
StudentRecord::StudentRecord(StudentRecord& ref){ // Copy constructor
*this = ref;
}
StudentRecord::~StudentRecord(){ // Destructor
delete[] m_firstName;
delete[] m_lastName;
}
StudentRecord& StudentRecord::operator=(StudentRecord& ref){ // Overloaded = operator
m_firstName = ref.m_firstName;
m_lastName = ref.m_lastName;
m_id = ref.m_id;
m_mark = ref.m_mark;
return *this;
}
bool StudentRecord::operator>(StudentRecord& ref){ // Overloaded > operator
if(m_mark > ref.m_mark)
return true;
else
return false;
} …