我需要在几个文件中使用std :: map变量。大多数示例都说,我需要在.h文件中将其声明为extern并在任何一个.cpp文件上初始化。但是我没有看到std :: map变量的更新值。
实施例: 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);
}
我们不要考虑每个State_Element中的内容以及它是如何初始化的。
的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);
}