0

when I have the following member in a class

    employee headOfDepartment;

what's wrong these setters and getters?

void department::setHeadOfDepartment( employee depEmployee)
    {
        headOfDepartment=depEmployee;
    }

employee department::getHeadOfDepartment() 
{
    return headOfDepartment;
}

I've been trying forever to define setters & getters with composition and it keeps getting me this error: "field ‘headOfDepartment’ has incomplete type"

ok Those are the header files:

   #ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
using namespace std;
#include <iostream>
#include <vector>
#include "department.h"
#include "project.h"
class department;
class project;

//#include <vector>

employee.h 

class employee
{
string Name; //text with spaces
string National_ID; //unique value (text) for each employee
static double Salary; // value of 1500 pounds
char Gender; //character holds f or m
int Available_vacations; //initially starts with 15 days
static double Deduction_per_day; // value of 85.5 pounds
int Available_permission_hours; //initially starts with 20 hours
static double Deduction_per_hour; // value of 15.5 pounds
double Actual_salary; // value with actual salary after deductions
int Vacations; // vacations employee took
int Permessions; // permession hours employee took
int empSerialNum; // object order in vector
department* myDepartment;
vector < project > empProjects;

public:
employee (); // default constructor
employee (string myName, string myNationalID, char myGender,int mySerialNum); // Parameterized constructor
~employee(); // Destractor

    //Setters
    void setName(string myName);
    void setNationalID (string myNationalID);
    void setGender (char myGander);
    void setAvailableVacation(int myAvVac);
    void setAvailablepermissionhours (int myAvPerHours);
    void setActualSalary (double actualSalary);
    void setVacations(int myVacations);
    void setPermessions(int myPermessions);
    void setempSerialNum(int mySerialNum);
    void setDepartment(department*);
    void addProject(project);

    //Getters
    string getName();
    string getNationalID ();
    char getGender ();
    int getAvailableVacation();
    int getAvailablepermissionhours ();
    double getActualSalary ();
    int getVacations ();
    int getPermessions ();
    int getempSerialNum();
    department* getDepartment();
    project* getProjects();



    void view (); // View to view Name, ID and actual salary

    void View_Detailed (); //call previous function and also shows other details (vacations - permissions - detailed deductions - ... )

    void Free_All(); //return all values to default

    double Take_vacation(); //this function takes number of days employee need to take as vacation, available vacations reduced by number of days given, if available vacations became 0 salary is deduced by deduction per day set

    double Take_permession(); //this function takes hours that employee asked to take, reduce available permission hour by hours given, if available permission become 0 hour salary is reduced by deduction per ho

    double Calculate_Actual_Salary();// calculates salary after deductions and returns it
};

#endif

department.h

#ifndef DEPARTMENT_H_
#define DEPARTMENT_H_
using namespace std;
#include <string.h>
#include "employee.h"
#include "project.h"
#include <vector>


class project;
class employee;

class department{

private:
    string name;
    string ID;
    employee headOfDepartment;


    vector <project> depprojects;  //projects managed by the department
public:

    //constructors
    department();
    department(string, string);


    //Setters
    void setName(string);
    void setID(string);
    void setHeadOfDepartment( employee /*const&*/ depEmployee);
    void addProject(project);

    //Getters
    string getName();
    string getID();
    employee getHeadOfDepartment() /*const*/;
//  project getProjects();

};

#endif

project.h

#ifndef PROJECT_H_
#define PROJECT_H_
#include <string.h>
#include "department.h"
#include "project.h"

class department;

class project{

    string name;
    department* location;

public:
    //constructors
    project();
    project(string proName, department* proDepartment);

    //Setters
    void setName(string proName);
    void setLocation(department* proDepartment);

    //Getters
    string getName();
    department* getLocation();

};


#endif
user2949483
  • 103
  • 1
  • 1
  • 8

3 Answers3

1

You need to the include the header file where employee is declared in your header and source files for department

shf301
  • 31,086
  • 2
  • 52
  • 86
  • I did actually include employee.h in both other files and still did not work. – user2949483 Nov 06 '13 at 19:26
  • 1
    Why do you have forward declarations for `class employee` and `class project`? Remove them - that's you issue. If you use forward declarations you can only reference a pointer to the class in your code. Forward declarations should only be used to break cycle dependency issues where you can't include the header file for a class. You don't seem to need them. – shf301 Nov 06 '13 at 19:43
  • when I remove them it gives me the error "‘employee’ does not name a type" – user2949483 Nov 06 '13 at 19:47
  • Ok you have you do have a cyclic dependency because `employee.h` includes `department.h` and `department.h` includes `employee.h`. You need to put back your forward declarations and change all references to `employee` in `department` to `employee*`. With forward declarations the compiler does not know the size of the object so you can only uses pointers to the object. – shf301 Nov 06 '13 at 19:52
  • Exactly! Thank you! I did change that to pointers in other classes -as you can see- but I thought "composition" won't apply here as a principle if I used pointers. Now that I know the reason it seems like it's indeed the only way. – user2949483 Nov 06 '13 at 20:00
1

Class employee shall be defined before using it as a type name of an object. Also I advice to add qualifier const for the getter

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You are not including the header that defines employee in your department class header, but you have a non-reference non-pointer declaration of type employee in your header.