2

我有一个链接器错误,我似乎不知道:

默认构造函数设置 name 为“Unknown”,Office Number 为 nextOfficeNo,Employee number 为 nextEmpId 的值,Department number 为 0,Employee Position 为 Entry level,工作年限为 0,salary 为 0。所有静态属性的值都加 1。

第二个构造函数根据传递给函数的内容设置属性。Employee Salary 的值仍将设置为 0,Office Number 和 Employee number 的值分别设置为 nextOfficeNo 和 nextEmpId。同样,构造函数应该将所有静态属性的值增加 1。totalNumOfEmployees 的值必须在创建任何对象之前初始化为 0,在创建 Employee 类的每个对象时递增(在每个构造函数中),并在 Employee 类的对象超出范围时递减(在析构函数中)。

nextEmpId 的值必须在创建任何对象之前初始化为 1000,并且必须在每个构造函数中创建 Employee 类的每个对象时递增。

nextOfficeNo 的值必须在创建任何对象之前初始化为 10,并且必须在每个构造函数中创建 Employee 类的每个对象时递增。

这是我的头类:

#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& );
 };

这是我的 CPP 课程:
我的构造函数有问题:

#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++;
}

以下是错误:

{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)
}
4

1 回答 1

3

您已经声明了静态成员变量,但您忘记定义它们:

§ 9.4.2/2静态数据成员在其类定义中的声明不是定义,并且可能是除 cv 限定的 void 之外的不完整类型。静态数据成员的定义应出现在包含该成员的类定义的命名空间范围内。在命名空间范围的定义中,静态数据成员的名称应使用 :: 运算符由其类名限定。静态数据成员定义中的初始化表达式在其类的范围内。

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

在你的情况下:

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

并从您的构造函数中删除这些分配:

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

否则,每次创建对象时,这些值都会被重置。

于 2012-11-09T05:34:17.570 回答