静态类成员,它是一个结构

Lon*_*nko 2 c++ static struct linker-errors stdvector

我有一个类,我希望有一个静态成员,它是一个结构.

例如:.h文件:

typedef struct _TransactionLog
{
    string Reference;
    vector<int> CreditLog;
    int id;
}TransactionLog;

class CTransactionLog {
    static TransactionLog logInfo;
public:
    static void Clear();
    static TransactionLog getLog();
};
Run Code Online (Sandbox Code Playgroud)

.cpp文件:

void CTransactionLog::Clear()
{
    logInfo.Reference = "";
    logInfo.CreditLog.clear();
 logInfo.id = 0;
}

TransactionLog CTransactionLog::getLog()
{
    return logInfo;
}
Run Code Online (Sandbox Code Playgroud)

我明白了

描述资源路径位置类型

未定义的对"CTransactionLog :: logInfo"TransactionLog.cpp的引用

有人可以举个例子来说明如何使这项工作成功吗?拥有一个静态成员是一个struct(带有stl成员),使用静态成员方法对其进行操作,并在代码的其他几个部分中包含此头.这应该用于通过应用程序添加日志记录.

Luc*_*ore 6

您需要在cpp文件中初始化静态成员:

//add the following line:
TransactionLog CTransactionLog::logInfo;

void CTransactionLog::Clear()
{
    logInfo.Reference = "";
    logInfo.CreditLog.clear();
 logInfo.id = 0;
}

TransactionLog CTransactionLog::getLog()
{
    return logInfo;
}
Run Code Online (Sandbox Code Playgroud)