Hey folks.

I am working on a project that adds additional information to previous projects. Basically, for ezch project i have added another header file with cpp file. For example, originally we just had an Employee.h and Employee.cpp file which contained a class for employees containing name, department, and SSN. Then we added more information etc etc.

I have got a problem with this line of code:

employee_list[numEmployees].setName(firstName, lastName);

This code calls this function within my Employee.cpp file (i included the Employee.h file in my main.cpp file where the function is called from - the header file includes arent the problem im 99% sure).

The called function looks like this:

void Employee::setName(const char * newFirst, const char * newLast)
{
setName(newFirst, newLast);   
}

The setName function - obviously calls itself - but i want to be able to call a setName function within the Name.h/Name.cpp file - so how can i call a function in a different file ???

If i leave my code like this - as soon as it tries to call this function, my output says
...
...
...
Segmentation Fault

Then nothing else - it stops... and exits the program run.

If you need more information i will give it to you - but im not what more you may need. Here is the Name.h file to give you an idea of what is included in the Name.cpp file.

#ifndef NAME_H
#define NAME_H

//#include "Employee.h"
#include <fstream>
#include <istream>
#include <iomanip>

using namespace std;

class Name
   {
   private:
           char firstName[11];
           char lastName[16];
           
   public:
          Name();
          void setName(const char *, const char *);
          const char *getFirstName();
          const char *getLastName();
          const void print();
          
   };
bool greaterThan(Name, Name);
#endif /* NAME_H */

I hope you guys could help. as i said , any questions, please ask and ill get back to you within a minute or 2.

Thanks

>>employee_list[numEmployees].setName(firstName, lastName);
Hummm -- is numEployees the max number of rows in the employee_list array? If yes then that is array overrun problem. My guess is it should be this: employee_list[numEmployees-1].setName(firstName, lastName); to access the 0 based element of the array.

To call the Name class, assuming Name is the base class of Employee.

void Employee::setName(const char * newFirst, const char * newLast)
{
     Name::setName(newFirst, newLast);   
}

Thank you for your input again. I tried the Name::getName(----); code and i got the following error message:

94 C:\Users\Tony\Documents\Employee.cpp cannot call member function `void Name::setName(const char*, const char*)' without object

I corrected my arrays to use this:

counter = 0;
employee_list[counter].getXXX();
..
..
..
counter += 1;

I also should note that you had a good point - at the end of the function - i use this line of code (because if the program is sent to this function - the idea is that a new employee needs to be added to the database)

i do: numEmployees += 1; (because the number of employees should be added (cant exceed 100 (max size of our databae according to rules).

Anyways - hope you could shed light on my problem some more... i realize i do not have an object like, for example:

NameObject.getName(firstName, lastName);

etc... but do i really need this? i thought the code you gave me using the :: should work - obviously not! Any tips?

It doesn't work because Name is not the base class of Employee. You can't call a method of another class unless (1) the method is a static method, or (b) there is an object of Name.

What are you attempting to do there? Maybe you need to make an object of Name inside the Employee class

class Employee
{
public:
    Name name;
...
};

Then you can call name.setName(...);

Wow - seeing as im not a complete idiot - i just realized i do have a Name name1; in my Employee.h file...

This should now work - we shall see - if i come across any REAL problems, ill repost here - thanks once again for your patience dragon. Your help is always appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.