我有这个有一个静态成员的类。它也是我程序中其他几个类的基类。这是它的头文件:
#ifndef YARL_OBJECT_HPP
#define YARL_OBJECT_HPP
namespace yarlObject
{
class YarlObject
{
// Member Variables
private:
static int nextID; // keeps track of the next ID number to be used
int ID; // the identifier for a specific object
// Member Functions
public:
YarlObject(): ID(++nextID) {}
virtual ~YarlObject() {}
int getID() const {return ID;}
};
}
#endif
这是它的实现文件。
#include "YarlObject.hpp"
namespace yarlObject
{
int YarlObject::nextID = 0;
}
我正在使用 g++,它返回三个undefined reference to 'yarlObject::YarlObject::nextID
链接器错误。如果我++nextID
将构造函数中的短语更改为 just nextID
,则只会出现一个错误,如果将其更改为1
,则链接正确。我想这很简单,但发生了什么?