小飞将
2019-10-03 23:48
采纳率: 66.7%
浏览 1.1k
已采纳

C++ 构造函数遇到“undefined reference to”一个静态数据成员的问题

如题:

#include <iostream>
#include <string>

class Employee {
private:
    static unsigned id;
public:
    Employee() { m_id = ++id;}
    Employee(const std::string &s) { m_id = ++id; m_name = s;}

    Employee(const Employee&) = delete;
    Employee &operator=(const Employee&) = delete;

    unsigned get_id() { return m_id;}
    std::string get_name() { return m_name;}
    static void init_id() { id = 0;}
private:
    unsigned m_id;
    std::string m_name;
};

int main() {
    Employee::init_id();
    Employee a("wu"), b("sun"), c("liu");
    std::cout << a.get_id() << " and " << a.get_name() << std::endl;
    std::cout << b.get_id() << " and " << b.get_name() << std::endl;
    std::cout << c.get_id() << " and " << c.get_name() << std::endl;
}

遇到error:
图片说明

这是什么原因呢?

2条回答 默认 最新

相关推荐 更多相似问题