0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    How do you compile your program? – Yksisarvinen Mar 29 '23 at 10:05
  • The thing is that you have to include *both* cpp files in the build of a complete program. How to do that depends a lot on what environment you are using (which you forgot to tell us). – BoP Mar 29 '23 at 10:25
  • I'd be surprised if `<<''<<` compiles (it certainly confuses stack overflows syntax highlighting). Are you actually compiling `Employee.cpp`? – Alan Birtles Mar 29 '23 at 10:28
  • It does not compile: https://godbolt.org/z/7PPMbT556 The `''` empty char is invalid and the class does not have `firstName` and `lastName` members – mch Mar 29 '23 at 10:57
  • I'm guessing that `<< ''` was meant to be `<< ' '` instead. – Remy Lebeau Mar 29 '23 at 17:08

1 Answers1

1
  1. You are not compiling the files in a proper manner, compiling it and executing it in a proper manner will resolve the issue.

    I used the clang compiler and I used the the following command.

    clang++ -std=c++11 main.cpp employee.cpp

  2. The other mistake is not including const string variables firstName and secondName. Including variables named firstName and lastName as demonstrated below will resolve this issue.

    class Employee
    {
    public:
        Employee( const string &, const string & ); // constructor
        ~Employee(); // destructor
        static int getCount(); // return number of objects instantiated
    
    private:
        const string firstName, lastName; //Declaring const string type variables firstName and lastName
        static int count; // number of objects instantiated
    
    }; // end class Employee
    
  3. You have to insert a whitespace in-between single-quotes in the cout statements in Employee.cpp:

    cout << "~Employee() called for " <<''<< lastName << endl;
    

    to

    cout << "~Employee() called for " <<' ' << lastName << endl;
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770