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

发布于 12-10 03:05 字数 760 浏览 2 评论 0原文

我有一个类,我想在其中有一个静态成员,它是一个结构。

例如: .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 成员),使用静态成员方法对其进行操作,并将此标头包含在代码的其他部分中。这应该用于通过应用程序添加日志记录。

I have a class in which I would like to have a static member which is a struct.

for example:
.h file:

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

class CTransactionLog {
    static TransactionLog logInfo;
public:
    static void Clear();
    static TransactionLog getLog();
};

.cpp file:

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

TransactionLog CTransactionLog::getLog()
{
    return logInfo;
}

I get

Description Resource Path Location Type

undefined reference to `CTransactionLog::logInfo' TransactionLog.cpp

Can someone please give me an example how to make this work? Having a static member which is a struct(with stl members), manipulate it with static member methods and include this header in few other parts of the code. This should be used to add logging through the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌2024-12-17 03:05:01

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

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

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

TransactionLog CTransactionLog::getLog()
{
    return logInfo;
}

You need to initialize your static member in the cpp file:

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

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

TransactionLog CTransactionLog::getLog()
{
    return logInfo;
}
前事休说2024-12-17 03:05:01

我是 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;
}

ma​​in.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
0

I'm a newbie in C/C++ and I have build it with Arduino IDE, sorry. The struts could be inside the class, to return the structure, it must be public, if the idea is only return the value, build it private.

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()
{
}

Output:

0
0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文