4

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.

Community
  • 1
  • 1
Lonko
  • 389
  • 9
  • 25
  • Off topic, but you shouldn't use reserved names like `_TransactionLog`. There's also no need for the typedef in C++ - class names are usable directly, without qualifying them with `struct` or `class`. – Mike Seymour Oct 13 '11 at 13:43
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Apr 28 '14 at 05:45

2 Answers2

8

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;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    You probably ment TransactionLog CTransactionLog::logInfo; – Lonko Oct 13 '11 at 13:13
  • @Luchian Grigore Could you please explain why we need to initialize that? What's the difference here? I think we do it just for static struct, do we? – MH.AI.eAgLe Jun 27 '22 at 05:12
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
ZiTAL
  • 3,466
  • 8
  • 35
  • 50