これが.hです:
class Logger
{
private:
static int mTresholdSeverity;
public:
static __declspec(dllexport) void log(const char* message);
static __declspec(dllexport) void logFormat(const char* format, ...);
static __declspec(dllexport) int getTresholdSeverity() { return mTresholdSeverity; }
static __declspec(dllexport) void setTresholdSeverity(int tresholdSeverity) { mTresholdSeverity = tresholdSeverity; }
};
そして.cpp:
#include "Logger.h"
#include <cstdarg>
int Logger::mTresholdSeverity = 200;
void Logger::log(const char* message)
{
//...
}
void Logger::logFormat(const char* format, ...)
{
//...
}
このエラーが発生します:
エラーLNK2001:未解決の外部シンボル "private:static int TransformationViewer_Utility_Logging :: Logger :: mTresholdSeverity"(?mTresholdSeverity @ Logger @ TransformationViewer_Utility_Logging @@ 0HA).. ..
明らかに、mTresholdSeverityは初期化されています。getTresholdSeverity()とsetTresholdSeverity()をコメントアウトするか、それらの定義を.cppファイルに移動すると、エラーは削除されます。
ヘッダーファイルで定義された静的メソッド(getTresholdSeverity()またはsetTresholdSeverity())が静的変数(mTresholdSeverity)を使用するときに、リンクエラーが発生するのはなぜですか?