我想確保一個模塊只加載只有一次,但是當我做我的方式,編譯器吐出「未定義參考我的兩個靜態類變量: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;
}
任何想法,爲什麼它說未定義參考兩個變量(實例&模塊)?
定義和類的實現是在同一個文件中還是在單獨的文件?錯誤消息指向哪裏? – gongzhitaao 2013-03-24 02:57:46
它指向包含該類定義的Cpp文件。標題和cpp文件是分開的。不提供行號。 – Brandon 2013-03-24 03:01:58
[未定義的引用靜態變量]的可能的重複(http://stackoverflow.com/questions/14331469/undefined-reference-to-static-variable) – jogojapan 2013-05-30 14:37:57