This is the error i am getting:
======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:28 2955225621 /home/w5/w5
...more
Aborted
My program compiles perfect, also part of my output is correct. I have 3 files, w5.cpp, employee.h, employee.cpp. I need to create a copy constructor, an assignment operator, and a destructor.
w5.cpp (its a little big). http://hostcode.sourceforge.net/view/1435.txt
employee.h class Employee {
int empNum;
char* empName;
public:
//Constructor
Employee();
Employee(int num, const char* name);
//Copy Constructor
Employee(const Employee& source);
//Assignment operator
Employee& operator=(const Employee& source);
//Destructor
~Employee();
void display() const;
bool isGreaterThan(const Employee&) const;
};
employee.cpp
#include <iostream>
using namespace std;
#include "Employee.h"
#include <string.h>
Employee::Employee() {
empNum = 0;
empName= nullptr;
}
Employee::Employee(int num, const char* name) {
if(num < 0 || strcmp(name,"")==0) {
Employee();
}
else {
empNum = num;
empName = new char[strlen(name)+1];
strcpy(empName, name);
}
}
Employee::Employee(const Employee& source) {
cout << "Copy Constructor!"<<endl;
if(source.empName !=nullptr) {
empNum = source.empNum;
empName = new char[strlen(source.empName)+1];
//strcpy(empName, source.empName);
empName = source.empName;
}
else
empName =nullptr;
}
Employee& Employee::operator=(const Employee& source) {
cout << "Operator Assignment!" <<endl;
// check for self-assignment
if(this != &source) {
cout << "Operator Assignment 2" <<endl;
empNum = source.empNum;
delete [] empName;
if(source.empName !=nullptr) {
empName = source.empName;
//strcpy(empName, source.empName);
}
else
empName = nullptr;
}
return *this;
}
//Destructor
Employee::~Employee() {
delete [] empName;
}
void Employee::display() const {
cout << empNum << empName <<endl;
}
bool Employee::isGreaterThan(const Employee& source) const {
return true;
//still need to code here
}
To compile this I use g++ -std=c++0x -o w5 w5.cpp Employee.cpp
After looking online regarding this problem I think I have a "non valid pointer" but i'm not sure where.