我有一个类,其中我希望有一个静态成员,它是一个结构。
例如:.h文件:
typedef struct _TransactionLog
{
string Reference;
vector<int> CreditLog;
int id;
}TransactionLog;
class CTransactionLog {
static TransactionLog logInfo;
public:
static void Clear();
static TransactionLog getLog();
};.cpp文件:
void CTransactionLog::Clear()
{
logInfo.Reference = "";
logInfo.CreditLog.clear();
logInfo.id = 0;
}
TransactionLog CTransactionLog::getLog()
{
return logInfo;
}我得到了
描述资源路径定位类型
对“`CTransactionLog::logInfo”TransactionLog.cpp的未定义引用
有人能给我举个例子吗?拥有一个结构的静态成员(带有stl成员),使用静态成员方法对其进行操作,并在代码的其他部分中包含这个标头。这应该用于通过应用程序添加日志记录。
发布于 2011-10-13 12:53:02
您需要在cpp文件中初始化静态成员:
//add the following line:
TransactionLog CTransactionLog::logInfo;
void CTransactionLog::Clear()
{
logInfo.Reference = "";
logInfo.CreditLog.clear();
logInfo.id = 0;
}
TransactionLog CTransactionLog::getLog()
{
return logInfo;
}发布于 2020-04-15 20:24:15
我是C/C++的新手,我用Arduino IDE构建了它,对不起。struts可以在类中,为了返回结构,它必须是公共的,如果这个想法只是返回值,那么构建它是私有的。
foo.h
class CTransactionLog
{
public:
struct TransactionLog
{
int id;
};
static void Clear();
static CTransactionLog::TransactionLog getLog();
static int getId();
private:
static CTransactionLog::TransactionLog _log_info;
};foo.cpp
#include "foo.h"
CTransactionLog::TransactionLog CTransactionLog::_log_info;
void CTransactionLog::Clear()
{
_log_info.id = 0;
}
CTransactionLog::TransactionLog CTransactionLog::getLog()
{
return _log_info;
}
int CTransactionLog::getId()
{
return _log_info.id;
}main.ino
#include "foo.h"
void setup()
{
Serial.begin(115200);
CTransactionLog::Clear();
CTransactionLog::TransactionLog log = CTransactionLog::getLog();
int id = CTransactionLog::getId();
Serial.println(log.id);
Serial.println(id);
}
void loop()
{
}输出:
0
0https://stackoverflow.com/questions/7754390
复制相似问题