2

我有这个有一个静态成员的类。它也是我程序中其他几个类的基类。这是它的头文件:

#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,则链接正确。我想这很简单,但发生了什么?

4

1 回答 1

1

确保您链接到生成的 .o 文件。仔细检查生成文件。

于 2010-05-26T21:17:31.577 回答