欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++ 静态变量

程序员文章站 2024-02-21 17:05:04
...

You will need to define the static variable, even if it is not initialized explicitly. That is what is missing in your code. You should have provided a simple example to reproduce the issue, but for your convenience I am providing one which works.

main.cpp

class Foo {
    public:
        static int si;
        static void bar();
};

int Foo::si = 0; // By default, it will be initialized to zero though.

void Foo::bar() {
     Foo::si = 10;
};

int main()
{
    Foo::bar();
    return 0;
}

 



===================================================================

The correct code is (.cpp):

#include "rtnamedinstance.h"

// Initialize static members
int RtNamedInstance::_nextInstanceNumber = 0;
QMutex RtNamedInstance::_syncObj(QMutex::Recursive);

RtNamedInstance::RtNamedInstance(QString instanceName)
{
    QMutexLocker locker(&_syncObj);

    // [... other code here ...]
}