【问题标题】:Apple mach O linkerApple mach O 链接器
【发布时间】:2012-11-09 05:11:10
【问题描述】:

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

默认构造函数将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)
}

【问题讨论】:

  • 你必须在某处定义那些静态成员。
  • 对此的更多讨论是here

标签: c++ xcode linker


【解决方案1】:

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

§ 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;

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

【讨论】:

  • 仍然失败,如果您查看错误,编译器会发现它们属于 Employee 类。
  • @EasyQuestions This 似乎相关...您是否在 .cpp 中的 #include "Employee.h" 下方添加了定义(int Employee::nextEmpId = 1000; 等)?
  • 现在,它可以工作了,非常感谢。你能告诉我为什么这和我之前的代码一样有效吗?
  • @EasyQuestions 静态成员变量有external linkage,因此必须在某处定义。声明(在类定义中)只告诉编译器应该使用这个变量,但实际上并没有“创建”它。如果您不定义它,链接器会意识到该变量是存在的,但找不到它并抱怨。将定义视为分配内存以保存变量的位置。使用该变量的所有内容都在访问同一块内存。
猜你喜欢
  • 2014-04-06
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
相关资源
最近更新 更多