loosing the data saved into an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jewel87
    New Member
    • Jan 2007
    • 62

    loosing the data saved into an object

    Hi,
    I have a problem with saving data to an object and displaying it. I am calling two functions, one to enter the data into object, and second to display that object data. When i try to debug the program using breakpoints, i see that the data is saved into the object attribute values, but when the display function is called, it sees and displays only the data to which the object is initialised by the class constructor. Basically the object attributes already contain the default values.
    Here is the code:

    Code:
    Unit1.cpp:
    
    #include "Unit1.h"
    #include "EmployeeList.h"
    
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    
    static int Size = 0;
    static int i = -1;
    
    void __fastcall TForm1::CreateEmployeeButtonClick(TObject *Sender)
    {
            theEmployeeList.addEmployee(i, Size);
    
            theEmployeeList.getEmployee(i).create(EnterEmployeeName,
                                                  EnterEmployeeSurname,
                                                  EnterEmployeeSocialNumber,
                                                  EnterEmployeeSalary);
    
            
    
            theEmployeeList.getEmployee(i).display(NameField,
                                                  SurnameField,
                                                  SocialNumberField,
                                                  SalaryField);
    
    
    }
    
    ___________________________________
    ___________________________________
    
    EmployeeList.cpp:
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Employee.h"
    #include "EmployeeList.h"
    
    //----------------------------------------------------------------------------
    
    Employee *myEmployeeList[10];
    
    static int ListSize=0;
    
    void EmployeeList :: addEmployee(int &i, int &SIZE)
    {
                    i = SIZE;
                    myEmployeeList[i]=new Employee;
                    SIZE++;
                    ListSize = SIZE;
    }
    
    Employee EmployeeList :: getEmployee(int index)
    {
            return *myEmployeeList[index];
    }
    
    ___________________________________
    ___________________________________
    
    Employee.cpp
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Employee.h"
    
    //---------------------------------------------------------------------------
    
    void Employee :: create(TEdit *EnterEmployeeName,
                            TEdit *EnterEmployeeSurname,
                            TEdit *EnterEmployeeSocialNumber,
                            TEdit *EnterEmployeeSalary)
    {
            //initialise data
            FirstName = EnterEmployeeName->Text;
            Surname = EnterEmployeeSurname->Text;
            Salary = StrToFloat(EnterEmployeeSocialNumber->Text);
            SocialNumber = StrToInt(EnterEmployeeSalary->Text);
    
    }
    
    
    void Employee :: display (TLabel *NameField,
                             TLabel *SurnameField,
                             TLabel *SocialNumberField,
                             TLabel *SalaryField)
    {
            (*NameField).Caption = FirstName;
            (*SurnameField).Caption = Surname;
            (*SocialNumberField).Caption = SocialNumber;
            (*SalaryField).Caption = Salary;
    }
    
    
    Employee :: Employee()
    {
            FirstName = "";
            Surname = "";
            SocialNumber = -1;
            Salary = -1;
    }
    
    Employee :: ~Employee()
    {
    }
    Please help me out!
  • jewel87
    New Member
    • Jan 2007
    • 62

    #2
    PEOPLE PLEASE HELP, i don't know what to do!

    Comment

    • Cucumber
      New Member
      • Sep 2007
      • 90

      #3
      Originally posted by jewel87
      PEOPLE PLEASE HELP, i don't know what to do!
      Code:
      return *myEmployeeList[index];
      You should always code copy constructors.

      Comment

      • jewel87
        New Member
        • Jan 2007
        • 62

        #4
        Originally posted by Cucumber
        Code:
        return *myEmployeeList[index];
        You should always code copy constructors.
        Sorry, not really getting your point, could you please explain a bit...
        Actually i've read some info on copy constructors, but not really getting how to define and use it here...

        Comment

        • Cucumber
          New Member
          • Sep 2007
          • 90

          #5
          Sorry, I took a fast look at your code and I saw your class lacks a copy constructor.
          But taking a second look, I realize that is not really the culprit of your problems.

          Code:
                  theEmployeeList.getEmployee(i).create(EnterEmploye  eName,
                                                        EnterEmployeeSurname,
                                                        EnterEmployeeSocialNumber,
                                                        EnterEmployeeSalary);
           
                  
           
                  theEmployeeList.getEmployee(i).display(NameField,
                                                        SurnameField,
                                                        SocialNumberField,
                                                        SalaryField);
          In the first line, the method getEmployee() is returning you an object created on the stack, this object is not the object you have in your list, its a copy of it; and a bad copy of it because you dont have a copy constructor.

          Anyway, you are not saving this object anywhere, and yet you modifiy it when calling the method create().

          In the second line, the method getEmployee() is returning you another object created on the stack, which is again, a copy of the object you have in your list. You call the method display(), which is useless because the modifications you made previously where not saved on the original object.

          The solution? Make getEmployee() return you a pointer or a reference.

          Comment

          • jewel87
            New Member
            • Jan 2007
            • 62

            #6
            Originally posted by Cucumber
            Sorry, I took a fast look at your code and I saw your class lacks a copy constructor.
            But taking a second look, I realize that is not really the culprit of your problems.

            Code:
                    theEmployeeList.getEmployee(i).create(EnterEmploye  eName,
                                                          EnterEmployeeSurname,
                                                          EnterEmployeeSocialNumber,
                                                          EnterEmployeeSalary);
             
                    
             
                    theEmployeeList.getEmployee(i).display(NameField,
                                                          SurnameField,
                                                          SocialNumberField,
                                                          SalaryField);
            In the first line, the method getEmployee() is returning you an object created on the stack, this object is not the object you have in your list, its a copy of it; and a bad copy of it because you dont have a copy constructor.

            Anyway, you are not saving this object anywhere, and yet you modifiy it when calling the method create().

            In the second line, the method getEmployee() is returning you another object created on the stack, which is again, a copy of the object you have in your list. You call the method display(), which is useless because the modifications you made previously where not saved on the original object.

            The solution? Make getEmployee() return you a pointer or a reference.

            First of all, thank you very much for your reply.
            I had getEmployee() as

            Code:
            Employee EmployeeList :: getEmployee(int index)
            {
                    return *myEmployeeList[index];
            }
            now i tried to change it to return a reference:

            Code:
            Employee EmployeeList :: getEmployee(int index)
            {
                    return &myEmployeeList[index];
            }
            but this is giving me an error E2034 Cannot convert 'Employee * *' to 'Employee'
            What am i doing wrong with returning a pointer or reference or what else should be changed there?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by jewel87
              Employee EmployeeList :: getEmployee(int index)
              {
              return &myEmployeeL ist[index];
              }


              but this is giving me an error E2034 Cannot convert 'Employee * *' to 'Employee'
              What am i doing wrong with returning a pointer or reference or what else should be changed there?
              Your function says to return an Employee. If you intend to return an Employee* then fix your return type so it matches what you are returning.

              Comment

              • jewel87
                New Member
                • Jan 2007
                • 62

                #8
                Thank you all guys very very much!!!! I finally got it working!

                Comment

                Working...