首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apple mach O链接器

Apple mach O链接器
EN

Stack Overflow用户
提问于 2012-11-09 13:11:11
回答 1查看 845关注 0票数 2

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

默认构造函数将name设置为“Unknown”,将Office Number设置为nextOfficeNo,将Employee number设置为nextEmpId的值,将Department number设置为0,将Employee Position设置为入门级别,将经验年份设置为0,将salary设置为0。它还确保所有静态属性的值都递增1。

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

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

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

这是我的头类:

代码语言:javascript
复制
#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类:

我在构造函数中遇到了这样的问题:

代码语言:javascript
复制
#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++;
}

以下是错误:

代码语言:javascript
复制
{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)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-09 13:34:18

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

§9.4.2/2静态数据成员在其类定义中的声明不是定义,可能是cv限定空以外的不完整类型。静态数据成员的定义应该出现在包含该成员的类定义的命名空间范围内。在命名空间范围的定义中,静态数据成员的名称应由其类名使用::运算符进行限定。静态数据成员定义中的初始值设定项表达式位于其类的作用域中。

代码语言:javascript
复制
// Example:
class process {
    static process* run_chain;
    static process* running;
};
process* process::running = get_main();
process* process::run_chain = running;

在您的案例中:

代码语言:javascript
复制
// add this to your .cpp
int Employee::totalNumofEmployees = 0;
int Employee::nextEmpId = 1000;
int Employee::nextOfficeNo = 10;

并从构造函数中删除这些赋值:

代码语言:javascript
复制
totalNumofEmployees = 0;
nextEmpId = 1000;
nextOfficeNo = 10;

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13302425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档