C++:在类中使用带有静态变量的 map 时遇到问题,我得到 undefined symbol

标签 c++ dictionary function-pointers

这是我得到的错误

Undefined symbols:
  "Config::fmap", referenced from:
      register_function(unsigned int (*)(unsigned int))in Config.o
      print_config()     in shared.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

配置.h

#include <map>
#include <boost/preprocessor/stringize.hpp>

typedef unsigned int (*fptr_t)(unsigned int);
typedef std::map<fptr_t, std::string> function_name_map_type;

void register_hash_names();
void register_function(fptr_t funct_pointer);

class Config
{
public:

    static function_name_map_type fmap;
};

配置.cpp

function_map_t fmap;


void register_hash_names()
{
    register_function(sha1);
    ...
}

void register_function(fptr_t funct_pointer)
{
    (Config::fmap)[funct_pointer] = BOOST_PP_STRINGIZE(funct_pointer);
}

错误来源Shared.cpp:

std::cout << "\t " << Config::fmap[Config::current_hash_function] << "\n";

最佳答案

.cpp 中的定义应该是:

function_map_t Config::fmap;

关于C++:在类中使用带有静态变量的 map 时遇到问题,我得到 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966434/

相关文章:

c++ - 指向类中方法的函数指针数组

c - 函数指针中标记之前的预期标识符

c++ - Python 依赖,windows (CMake)

c++ - vector::iterator也可以用作 vector 吗

C++ 静态初始化顺序 : adding into a map

c++ - C语言中如何在循环或IF语句中一次重复一次?

python 将 csv 文件读取为字典并排序和递增计数器

scala - scala 中映射的按键聚合/归约函数

python - 合并两个字典列表,将值加在一起

c - 成员方法有 C 宏吗?