Zan*_*nko 1 c++ dictionary declaration definition
Value.h
#ifndef VALUE_H
#define VALUE_H
#include <map>
#include <string>
std::map<uint8_t, std::string> status;
#endif
Run Code Online (Sandbox Code Playgroud)
Value.cpp
#include "Value.h"
std::map<uint8_t, std::string> status = {
{ 0x00, "Status1" },
{ 0x11, "Status2" },
{ 0x10, "Status3" }
};
Run Code Online (Sandbox Code Playgroud)
但我一直在重新定义; 多次初始化错误; 我该如何申报和初始化?我希望地图是全球性的.可以不将地图包装在一个类中吗?我只是希望这个文件是其他文件的地图和值的信息.
注意std::map<uint8_t, std::string> status;仍然是一个定义,即使没有初始化器(即空映射).
您可以使用extern使其成为声明Value.h;
extern std::map<uint8_t, std::string> status;
Run Code Online (Sandbox Code Playgroud)
此外,使用extern且没有初始值设定项的变量声明不是定义.