0

I'm trying to overload the '+' and '=' operators in my code, but I keep getting a run time error and the program crashes when running using VS2012 but runs perfectly in borland C 3.1.

Here's my code:

class employee{
    int eid;
    long esalary;
    char * ename;
    static char company_name[20];
    static int emp_count;

    public:

    static char * getcompanyname(){
        return company_name;
    }
    static int getempcount(){
        return emp_count;
    }
    void set(int empid);
    void set(long empsalary);
    void set(char empname[]);
    int getid();
    long getsalary();
    char * getname();
    employee(int empid=0,long empsalary=0,char empname[]="NA"){
        eid=empid;
        esalary=empsalary;
        ename=new char[strlen(empname)+1];
        strcpy(ename,empname);
        emp_count++;
    }

    employee(employee &ref){
        eid=ref.eid;
        esalary=ref.esalary;
        ename=new char(strlen(ref.ename)+1);
        strcpy(ename,ref.ename);
    }

    ~employee(){
        delete(ename);
    }

    employee operator+(employee &ref){
        employee temp(*this);
        temp.esalary=esalary+ref.esalary;
        return(temp);
    }
    employee& operator= (employee &ref){
        eid=ref.eid;
        esalary=ref.esalary;
        return * this;
    }

}e1,e2,emp;

then in main:

emp=e1+e2;
5
  • 1
    Please post run time error info for VS2012. Also please consider revising the title of your question to reflect what the question is about in a more specific way. Commented Dec 25, 2012 at 20:25
  • Are you overloading them inside the class? And where is that last line being made? Commented Dec 25, 2012 at 20:25
  • PS: The implementations are defined within the class...and the last line is in the main function Commented Dec 25, 2012 at 20:25
  • How is operator[] overloaded? Commented Dec 25, 2012 at 20:26
  • Maybe if you show us a bit more code - where does e[] come from, for example? Borland C, are you running in DOS? IN which case you probably don't get crashes, just weird behaviour if you go outside of what your allocated memory is. If e[] has three elemnts, e[3] is invalid! Commented Dec 25, 2012 at 20:26

1 Answer 1

3

To be honest, your code is invalid. It should not even compile, since it violates the reference binding rules: the + operator returns a temporary object, which cannot be passed through a non-const reference to the = operator. If you managed to compile this code, it simply means that your compiler(s) accepts it as an "extension" of the language.

To fix that specific error you have to add a bunch of const qualifiers to your declarations

employee operator +(const employee &ref) const {
  employee temp(*this);
  temp.esalary = esalary + ref.esalary;
  return temp;
}

employee& operator =(const employee &ref){
  eid = ref.eid;
  esalary = ref.esalary;
  return *this;
}

This will make your code valid from the C++ point of view, but it probably won't fix the crash, since the reason for the crash must be elsewhere.


Here's your crash-causing error: in the copy-constructor you did this

ename=new char(strlen(ref.ename)+1);

When you allocate an array with new, you have to use [] brackets, not ()

ename = new char[strlen(ref.ename) + 1];

You did it correctly in your first constructor, but then you for some reason used () instead of [] in your copy constructor. () in this context mean something completely different: it allocates a single char and initializes it to strlen(ref.ename) + 1 value.

BTW, is there a reason you are not copying ename in the copy-assignment operator?

Also, memory allocated with new[] must be freed with delete[]. Not with delete, but with delete[]. This is what your destructor should look like

~employee() {
  delete[] ename;
}

Finally, you might be much better off using std::string for storing ename, instead of relying on raw memory management. (Unless you were specifically asked to do it that way).

Sign up to request clarification or add additional context in comments.

2 Comments

I was copying ename but then I removed it as I thought it was the error source....and sorry but what is std::string?
@Mazen: std::string is a class provided by C++ standard library. It is intended to store string values. It takes care of all the memory management internally, so that you don't have to do it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.