gpt4 book ai didi

c++ - 静态变量 undefined reference

转载 作者:行者123 更新时间:2023-11-28 03:20:27 25 4
gpt4 key购买 nike

我试图确保一个模块只加载一次,但是当我按照我的方式执行时,编译器吐出“对我的两个静态类变量的 undefined reference :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;
}

知道为什么它说对两个变量(实例和模块)的 undefined reference 吗?

最佳答案

您应该在使用之前定义您的静态变量。

添加

int Text::Instancese = 0 // whatever value you need.

在您的 .cpp 文件的顶部。

关于c++ - 静态变量 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com