初始化静态地图,不知道密钥
initialize a static map without know the key
是否可以初始化静态地图而不知道键?
我有一个我使用
的A类static std::map<ObjClass *, int> n_map;
,所以我需要在此类中初始化它,但是由于我不知道对象的内容objclass,我得到了错误:
undefined reference to A::n_map
我该如何解决?
静态类数据成员需要定义并被声明。初始化器可以使用非恒定表达构件的定义,也可以使用恒定表达的声明。
例如:
foo.h:
#include <map>
#include <string>
struct Foo
{
// declaration and initializer
static constexpr int usageCount = 0;
// only declaration
static const double val1;
static double val2;
// only declaration
static std::map<int, std::string> theMap;
};
foo.cpp:
#include <foo.h>
// only definition (only required if ODR-used)
constexpr int Foo::usageCount;
// definition and initializer (initializer required, since const)
const double Foo::val1 = 1.5;
// only definition (implicitly statically-initialzied (i.e. zeroed))
double Foo::val2;
// definition and initializer
std::map<int, std::string>> Foo::theMap { { 1, "abc" }
, { 2, "def" }
};
相关文章:
- curl_easy_perform() 失败:SSL 对等证书或 SSH 远程密钥不正常
- 运行密钥密码解密知道密钥?
- C++密钥和文件的映射不起作用
- Nlohmann 在不知道密钥的情况下解析 JSON 文件
- 为什么 std::map 接受 std::p air 作为密钥,而 std::unordered_map 不接受?
- 使用char*作为undered_map的密钥不会识别重复的密钥
- Xsendevent不会更改修饰符密钥状态
- 初始化静态地图,不知道密钥
- 使用数组作为地图密钥不使用C 11编译器命令
- 在不使用密钥的情况下有效地搜索哈希表
- 在不禁用其密钥的情况下注册全局热键
- SDL_GetKeyboardState 不会返回密钥的当前状态
- 向启动添加内容的代码不会生成密钥
- ECDH共享密钥在Crypto++和Android之间不匹配
- 尝试多个密钥进行 Caesar 密码解密从密钥 9 而不是 1 开始
- ssl对端证书或ssh远程密钥不正确
- 了解找不到密钥时QHash的作用
- C++:在不知道密钥的情况下插入std::map
- SecByteBlock 不接受字符串作为自定义密钥
- WebSocket服务器实现(接受密钥不匹配)