Employee::Employee(const Employee& other):name(other.name), id(count) { ++co
- C/C++ code
//为什么出现//undefined reference to `Employee::count'//.hclass Employee{ public: Employee():name("NoName"), id(count) { ++count; } Employee(std::string strName):name(strName), id(count) { ++count; } virtual ~Employee(); Employee(const Employee& other); Employee& operator=(const Employee& other); protected: private: std::string name; int id; static int count;};//.cppEmployee::Employee(const Employee& other):name(other.name), id(count){ ++count;}[解决办法]
类的static成员变量,必须在类的定义之外定义一次。
在 .cpp 中加上一行
int Employee::count = 0;
[解决办法]
另:不要忘了析构函数的实现,哪怕是空实现