我的这个类有一个静态成员。它也是我程序中其他几个类的基类。这是它的头文件:
#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
短语更改为 nextID
,那么我只会收到一个错误,如果我将其更改为 1
,然后它会正确链接。我想这很简单,但这是怎么回事?
最佳答案
确保您链接的是生成的 .o 文件。仔细检查 makefile。
关于c++ - 对静态成员变量的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916759/