C++ 苹果mach O连接器

C++ 苹果mach O连接器,c++,xcode,linker,C++,Xcode,Linker,我有一个链接器错误,我似乎没有弄明白: 默认构造函数将名称设置为“未知”,将办公室编号设置为nextoficeno,将员工编号设置为nextEmpId的值,将部门编号设置为0,将员工职位设置为入门级,将工作年限设置为0,将薪资设置为0。它还确保所有静态属性的值都增加1 第二个构造函数根据传递给函数的内容设置属性。Employee Salary的值仍将设置为0,Office Number和Employee Number的值将分别设置为nextOfficeNo和nextEmpId。同样,构造函数应该

我有一个链接器错误,我似乎没有弄明白:

默认构造函数将名称设置为“未知”,将办公室编号设置为nextoficeno,将员工编号设置为nextEmpId的值,将部门编号设置为0,将员工职位设置为入门级,将工作年限设置为0,将薪资设置为0。它还确保所有静态属性的值都增加1

第二个构造函数根据传递给函数的内容设置属性。Employee Salary的值仍将设置为0,Office Number和Employee Number的值将分别设置为nextOfficeNo和nextEmpId。同样,构造函数应该将所有静态属性的值增加1。 也;TotalNumofEmployee的值必须在创建任何对象之前初始化为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& );
 };
#包括
#包括
使用名称空间std;
班级员工
{
私人:
字符串名;
警察长办公室;
康斯特朗恩皮德;
int deptNo;
char empPosition;//“E”:入门级,“M”:经理,“D”:主管,“P”:项目负责人
国际经验年;
浮动工资;
员工的静态整数;
静态int-nextEmpId;
静态int nextOfficeNo;
公众:
雇员();
~Employee();
Employee(字符串名称,int the Deptno,char the employment,int the Year of Exp);
无效打印()常量;
void GetInfo();
friend void setSalary(员工&);
};
这是我的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++;
}
totalNumofEmployees = 0;
nextEmpId = 1000;
nextOfficeNo = 10;
#包括“Employee.h”
#包括
#包括
雇员::雇员()
:officeNo(nextoficeno)、empId(nextEmpId)
{
name=“未知”;
deptNo=0;
empPosition='E';
yearOfExp=0;
工资=0;
员工总数=0;
nextEmpId=1000;
nextoficeno=10;
员工总数++;
nextEmpId++;
nextoficeno++;
}
Employee::Employee(字符串名称,int theDeptNo,char theemployment,int theYearOfExp)
:officeNo(nextoficeno)、empId(nextEmpId)
{
名称=名称;
deptNo=deptNo;
empPosition=empPosition;
经验年=经验年;
工资=0;
员工总数=0;
nextEmpId=1000;
nextoficeno=10;
员工总数++;
nextEmpId++;
nextoficeno++;
}
以下是错误:

{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)
}
{架构x86_64的未定义符号:
“Employee::nextOfficeNo”,引用自:
Employee.o中的Employee::Employee()
Employee.o中的Employee(std::uu 1::basic_字符串,int,char,int)
“Employee::totalNumofEmployees”,引用自:
Employee.o中的Employee::Employee()
Employee.o中的Employee(std::uu 1::basic_字符串,int,char,int)
Employee.o中的Employee::~Employee()
Employee::Employee.o中的Print()常量
“Employee::nextEmpId”,引用自:
Employee.o中的Employee::Employee()
Employee.o中的Employee(std::uu 1::basic_字符串,int,char,int)
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
}

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

§9.4.2/2静态数据成员在其类定义中的声明不是一个定义,可以是 除cv合格无效外的不完整类型。静态数据成员的定义应出现在 包含成员的类定义的命名空间范围。在命名空间范围的定义中,名称 静态数据成员的类名称应使用::运算符限定。初始值设定项 静态数据成员定义中的表达式在其类的范围内

就你而言:

// add this to your .cpp
int Employee::totalNumofEmployees = 0;
int Employee::nextEmpId = 1000;
int Employee::nextOfficeNo = 10;
并从构造函数中删除这些分配:

#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++;
}
totalNumofEmployees = 0;
nextEmpId = 1000;
nextOfficeNo = 10;

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

您必须在某个地方定义这些静态成员。对此的进一步讨论仍然失败,如果您查看错误,编译器会发现它们属于Employee类。@EasyQuestions似乎相关。。。您是否在.cpp中的
#include“Employee.h”
下面添加了定义(
int Employee::nextEmpId=1000;
等)?现在,它可以工作了,非常感谢。你能告诉我为什么这和我之前的代码一样有效吗?@EasyQuestions静态成员变量已经存在,因此必须在某个地方定义。声明(在类定义中)只告诉编译器应该使用这个变量,但实际上并不“创建”它。如果您没有定义它,链接器会意识到该变量本来就存在,但找不到它,并发出抱怨。将定义视为分配内存以保存变量的位置。使用该变量的所有内容都在访问相同的内存块。