我有一个链接器错误,我似乎不知道:
默认构造函数设置 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)
}