2

I have a linker error and I don't seem to figure out:

The default constructor sets name to “Unknown”, Office Number to nextOfficeNo, Employee number to the value of nextEmpId, Department number to 0, Employee Position to Entry level, year of experience to 0, and salary to 0. It also ensures that the values of all static attributes are incremented by 1.

The second constructor sets the attributes based on what is passed to the function. The value of Employee Salary would still be set to 0 and the values of Office Number and Employee number are set to nextOfficeNo and nextEmpId, respectively. Again, the constructor should increment the values of all static attributes by 1. also; The value of totalNumOfEmployees must be initialized to 0 before creating any object, be incremented upon creating each object of class Employee (in every constructor), and be decremented when an object of class Employee goes out of scope (in destructor).

The value of nextEmpId must be initialized to 1000 before creating any object, and must be incremented upon creating each object of class Employee in every constructor.

The value of nextOfficeNo must be initialized to 10 before creating any object, and must be incremented upon creating each object of class Employee in every constructor.

this is my header class:

#include <iostream>
#include <string>
using namespace std;

class Employee
{
private:
    string name;
    const long officeNo;
    const long empId;
    int deptNo;
    char empPosition; // ‘E’: entry level, ‘M’: manager, ‘D’: Director, ‘P’:Project_leader
    int yearOfExp;
    float salary;
    static int totalNumofEmployees;
    static int nextEmpId;
    static int nextOfficeNo;
public:
    Employee();
    ~Employee();
    Employee(string theName, int theDeptNo, char theEmpPosition, int theYearOfExp);
    void Print() const ;
    void GetInfo();
    friend void setSalary(Employee& );
 };

and this is my CPP class:
I have the problems in my constructors:

#include "Employee.h"
#include <string>
#include <iostream>

Employee::Employee()
  : officeNo(nextOfficeNo), empId(nextEmpId)
{
    name = "Unknown";
    deptNo = 0;
    empPosition = 'E';
    yearOfExp = 0;
    salary = 0;
    totalNumofEmployees = 0;
    nextEmpId = 1000;
    nextOfficeNo = 10;
    totalNumofEmployees++;
    nextEmpId++;
    nextOfficeNo++;
}

Employee::Employee(string theName, int theDeptNo, char theEmpPosition, int theYearOfExp)
  : officeNo(nextOfficeNo), empId(nextEmpId)
{
    name = theName;
    deptNo = theDeptNo;
    empPosition = theEmpPosition;
    yearOfExp = theYearOfExp;
    salary = 0;
    totalNumofEmployees = 0;
    nextEmpId = 1000;
    nextOfficeNo = 10;
    totalNumofEmployees++;
    nextEmpId++;
    nextOfficeNo++;
}

here are the errors:

{Undefined symbols for architecture x86_64:
"Employee::nextOfficeNo", referenced from:
Employee::Employee() in Employee.o
Employee::Employee(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, char, int) in Employee.o
"Employee::totalNumofEmployees", referenced from:
Employee::Employee() in Employee.o
Employee::Employee(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, char, int) in Employee.o
Employee::~Employee() in Employee.o
Employee::Print() const in Employee.o
"Employee::nextEmpId", referenced from:
Employee::Employee() in Employee.o
Employee::Employee(std::__1::basic_string<char, std::__1::char_traits<char>,  std::__1::allocator<char> >, int, char, int) in Employee.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
}
Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
EasyQuestions
  • 327
  • 1
  • 10
  • 23

1 Answers1

3

You have declared static member variables but you forgot to define them:

§ 9.4.2/2 The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class.

// Example:
class process {
    static process* run_chain;
    static process* running;
};
process* process::running = get_main();
process* process::run_chain = running;

In your case:

// add this to your .cpp
int Employee::totalNumofEmployees = 0;
int Employee::nextEmpId = 1000;
int Employee::nextOfficeNo = 10;

And remove these assignments from your constructors:

totalNumofEmployees = 0;
nextEmpId = 1000;
nextOfficeNo = 10;

Otherwise, ever time you create an object, these values are reset.

Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
  • still fails, if you look at the errors, the compiler figures out that they belong to the Employee class. – EasyQuestions Nov 09 '12 at 06:25
  • @EasyQuestions [This](http://stackoverflow.com/questions/12105377/static-c-variable-apple-mach-o-linker-issue) seems related... Did you add the definitions (`int Employee::nextEmpId = 1000;` etc) below the `#include "Employee.h"` in your .cpp? – Anonymous Coward Nov 09 '12 at 07:13
  • now, it works, thanks very much. Can you tell me why this works as apposed to the code that I had earlier? – EasyQuestions Nov 09 '12 at 08:11
  • @EasyQuestions A static member variable has [external linkage](http://stackoverflow.com/q/1358400/1764118) and therefore must be defined somewhere. The declaration (in the class definition) only tells the compiler that this variable should be used but does not actually "create" it. If you don't define it, the linker realizes that the variable is meant to exist but cannot find it and complains. Think of the definition as the place where memory is allocated to hold the variable. Everything using that variable is accessing the same block of memory. – Anonymous Coward Nov 09 '12 at 08:42