静的メンバーを持つこのクラスがあります。これは、私のプログラムの他のいくつかのクラスの基本クラスでもあります。そのヘッダーファイルは次のとおりです。
#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++ を使用していますが、3 つのundefined reference to 'yarlObject::YarlObject::nextID
リンカー エラーが返されます。コンストラクターの++nextID
フレーズを justnextID
に変更すると、エラーが 1 つだけ発生し、それを に変更すると1
、正しくリンクされます。簡単なことだと思いますが、どうなんでしょう?