Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 静态变量未定义引用_C++ - Fatal编程技术网

C++ 静态变量未定义引用

C++ 静态变量未定义引用,c++,C++,我试图确保一个模块只加载一次,但当我按照自己的方式加载时,编译器会抛出“对我的两个静态类变量的未定义引用:S” class Text : public Parent { private: static int Instances; static HMODULE Module; public: Text(); Text(Text&& T); Text(std::wstring T);

我试图确保一个模块只加载一次,但当我按照自己的方式加载时,编译器会抛出“对我的两个静态类变量的未定义引用:S”

class Text : public Parent
{
    private:
        static int Instances;
        static HMODULE Module;

    public:
        Text();
        Text(Text&& T); 
        Text(std::wstring T); 
        ~Text();

        virtual Text& operator = (Text&& T); 
};

Text::Text() : Parent() {}

Text::~Text()
{
    if (--Instances == 0)
        FreeLibrary(Module); // Only free the module when
                             // no longer in use by any instances.
}

Text::Text(Text&& T) : Parent(std::move(T)), Module(std::move(T.Module))

Text::Text(std::wstring T) : Parent(T)  // Module only loads when
                                        // this constructor is called.
{
    if (++Instances == 1)
    {
        Module = LoadLibrary(_T("Msftedit.dll"));
    }
}

Text& Text::operator = (Text&& T)
{
    Parent::operator = (std::move(T));
    std::swap(T.Module, this->Module);
    return *this;
}

你知道为什么它说两个变量(实例和模块)都是未定义的引用吗?

在使用它之前,你应该定义你的静态变量


.cpp
文件的顶部。

类定义声明了两个静态数据成员,
Text::Instances
Text::Module
。如果代码实际使用这些数据成员,还必须定义它们。就像
void f();
声明了一个函数,但除非同时定义它,否则不能调用它。因此添加:

int Text::Instances;
HMODULE Text::Module;

(这与当前代码是完全写在头中还是拆分为头和源代码无关;如果使用它,则必须定义它)

该类的定义和实现是在同一个文件中还是在不同的文件中?错误消息指向哪里?它指向包含该类定义的Cpp文件。头文件和Cpp文件是分开的。没有行号。可能重复Wow..成功..原因是:我不明白你为什么EcLARE是实现文件中的一个变量。我以前从来没有见过这个。@ CouthDeXeUsReNe:这是C++:我想C++上的任何介绍都有一个解释:D谷歌IT。@ CantChooseUsernames,你不声明它。你定义它。@康拉德鲁道夫谢谢你的措辞。
int Text::Instances;
HMODULE Text::Module;