未定义的静态成员变量引用 [英] undefined reference to static member variable

查看:567
本文介绍了未定义的静态成员变量引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this class that has a static member. it is also a base class for several other classes in my program. Here's its header file:

#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

这里是它的实现文件。

and here's its implementation file.

#include "YarlObject.hpp"

namespace yarlObject
{
    int YarlObject::nextID = 0;
}



我使用g ++,它返回三个未定义的引用'yarlObject :: YarlObject :: nextID 链接器错误。如果我将构造函数中的 ++ nextID 短语改为 nextID ,那么我只得到一个错误,如果我将其更改为 1 ,然后它正确链接。

I'm using g++, and it returns three undefined reference to 'yarlObject::YarlObject::nextID linker errors. If I change the ++nextID phrase in the constructor to just nextID, then I only get one error, and if I change it to 1, then it links correctly. I imagine it's something simple, but what's going on?

推荐答案

确保您正在连接生成的.o文件。仔细检查makefile。

Make sure you are linking against the generated .o file. Double-check the makefile.

这篇关于未定义的静态成员变量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆