This is my main file:
#include <iostream>
#include"Employee.h"
using namespace std;
int main()
{
cout << "Number of employees before instantiation of any objects is " << Employee::getCount() << endl;
return 0;
}
This is my Employee.h
file:
#ifndef EMPLOYEE_H
#define EMPLOYEE _H
#include <string>
using namespace std;
class Employee
{
public:
Employee( const string &, const string & ); // constructor
~Employee(); // destructor
static int getCount(); // return number of objects instantiated
private:
static int count; // number of objects instantiated
string firstName;
string lastName;
}; // end class Employee
#endif
And this is my Employee.cpp
file:
#include <iostream>
#include "Employee.h" // Employee class definition
using namespace std;
int Employee::count = 0; // cannot include keyword static
Employee::Employee( const string &first, const string &last )
: firstName( first ), lastName( last )
{
++count; // increment static count of employees
cout << "Employee constructor for " << firstName << ' ' << lastName << " called." << endl;
} // end Employee constructor
// destructor deallocates dynamically allocated memory
Employee::~Employee()
{
cout << "~Employee() called for " << firstName << ' ' << lastName << endl;
--count;
} // end ~Employee destructor
int Employee::getCount()
{
return count;
}
Does anyone know why there is an error in my main()
function?
undefined reference to `Employee::getCount()'
I have declared the static data member and the static member function inside the class, and define them outside the class. I have also included Employee.h
in my main file and Employee.cpp
file, but the program still does not run.