我有一个链接器错误,我似乎没弄明白:
默认构造函数将name设置为“Unknown”,将Office Number设置为nextOfficeNo,将Employee number设置为nextEmpId,将Department number设置为0,将Employee Position设置为Entry,将经验年份设置为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)
}
答案 0 :(得分:3)
您已声明静态成员变量,但您忘记定义它们:
§9.4.2/ 2 静态数据成员在其类定义中的声明不是定义,可能是 除了cv-qualified 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;
否则,无论何时创建对象,都会重置这些值。