C++ 全局std::c+中的map变量+;
我需要在多个文件中使用std::map变量。大多数示例都说,我需要在.h文件中将其声明为extern,并在任何一个.cpp文件上初始化。但是我没有看到std::map变量上的更新值 例如: 配置.hC++ 全局std::c+中的map变量+;,c++,global-variables,stdmap,C++,Global Variables,Stdmap,我需要在多个文件中使用std::map变量。大多数示例都说,我需要在.h文件中将其声明为extern,并在任何一个.cpp文件上初始化。但是我没有看到std::map变量上的更新值 例如: 配置.h struct Action_Element { std::string algo_func_name; uint32_t opEventUid; }; typedef std::map<int, Action_Element> EnActionList; typedef
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
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);
}
让我们不要考虑每个状态元素中的内容以及它是如何初始化的。
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);
}
复制品:@Angelus那怎么是复制品?OP在标题中声明了它
extern
,并提供了一个定义。
#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);
}