2

I need to use a std::map variable across several files. Most of the example says, I need to declare it as extern on .h file and initialize on any one of the .cpp file. But I don't see updated value on the std::map variable.

Example: Config.h

struct Action_Element {
    std::string algo_func_name;
    uint32_t opEventUid;
};

typedef std::map<int, Action_Element> EnActionList;
typedef std::map<int, Action_Element> ExActionList;
typedef std::map<size_t, string> CondExpStrList;

struct Transition_Elements {
    uint32_t              DesStateUid;
    uint32_t              TransCounterID;
    ExActionList          ExitActionList;
};

typedef std::map<uint32_t, Transition_Elements> TransitionList;

struct State_Element {
    bool              isTransitionCondAvailable;
    EnActionList      EntryActionList;
    TransitionList    TransitionList;
};

typedef std::map<uint32_t , State_Element> StateList;
extern StateList global_stateList;     // global state list

file1.cpp

#include "Config.h"

file1::file1()
{
   State_Element  file1StateElement;
   global_statList.insert(make_pair(1,file1StateElement);
}

file3.cpp

#include "Config.h"

file3::file3()
{
   State_Element  file3StateElement;
   global_statList.insert(make_pair(3,file3StateElement);
}

file2.cpp

#include "Config.h"

file2::file2()
{
   State_Element  file2StateElement;
   global_statList.insert(make_pair(2,file2StateElement);
}

Let us not consider whats inside each State_Element and how it is initialized.

main.cpp

#include "Config.h"
StateList global_stateList;

int main(void) {

global_stateList.clear();
file1 *f1 = new file1();
file2 *f2 = new file2();
file3 *f3 = new file3();

StateList::const_iter iter = global_stateList.begin();
if(iter != global_stateList.end())
{
   printf("\n size of globalstateList : %d", global_stateList.size());
}

while(1);

}
kar
  • 495
  • 1
  • 8
  • 19

0 Answers0