What did I do wrong when coding this array of objects in C ++?

I have two classes: PersonnelLists and Employee. I create an instance of PersonnelLists in my main form, for example:

int main() { PersonnelLists example; //Make a personnel list ... } 

PersonnelLists uses a constructor to initialize a member of the employee list, number of employees, and array size:

 PersonnelLists::PersonnelLists(): List(new Employee[SIZE]), numEmployees(0), arraySize(SIZE){ } 

This leads to the creation of empty zero employees (I think?):

 Employee::Employee(): employeeNumber(0), name(NULL), department(NULL) { } 

On this line, I get an invalid null pointer error.

I am new to C ++, new to Java program. I'm still new to pointers, so I'm not quite sure what I'm doing wrong here.

UPDATE: As requested, here is the definition of the Employee class:

 #include <iostream> class Employee { public: Employee(); //constructor Employee(std::string name, std::string deparment); void Print() const; //Print this employee details void setEmployeeNo(int employeeNum); private: int employeeNumber; std::string name; std::string department; }; 
+4
source share
3 answers

Java's new Employee[SIZE] creates an array of null references.

In C ++, new Employee[SIZE] creates an array of Employee instances built by default. Your default constructor is trying to set name and department to null . Trying to initialize a std::string to null will give the error you described.

In C ++ there is no "null" line, but you can default the name and department construct, which sets them to empty lines:

 Employee::Employee(): employeeNumber(0), name(), department() { 

Finally, if the List can contain a variable number of elements, I would recommend using std::vector<Employee> (which is similar to ArrayList<Employee> in Java).

+3
source

If name and department are std::string (or a similar string type), then initializing them with NULL (a null character pointer) is invalid.

If I guessed correctly, you should initialize them by default, like:

 Employee::Employee(): employeeNumber(0), name(), department() { } 

But we really cannot say without seeing the definition of the Employee class.

As others have pointed out, std::vector should be used instead of an array. This allows you to have valid Employee objects in your "list".

+1
source

I do not know what the actual definitions of your classes are, so it is difficult to determine your problem.

But an option in modern C ++ is to use the std::vector<Employee> data element inside the PersonnelList class. std::vector can grow dynamically at runtime using its push_back() method, for example

 #include <vector> // for std::vector class Employee { .... }; class PersonnelList { public: PersonnelList() { // Nothing to do - vector is initialized empty } // Get current employee count size_t Count() const { return m_employees.size(); } // Add a new employee to the personnel void AddEmployee(const Employee& newEmployee) { m_employees.push_back(newEmployee); } private: std::vector<Employee> m_employees; }; 

No need to use raw pointers or the like: robust RAII STL container classes simplify your code.

+1
source

All Articles