开发者

C++: Having trouble using map with a static variable in a class, I get undefined symbol

开发者 https://www.devze.com 2023-03-05 16:25 出处:网络
This is the error I get Undefined symbols: \"Config::fmap\", referenced from: register_function(unsigned int (*)(unsigned int))in Config.o

This is the error I get

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

Config.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;
};

Config.cpp

function_map_t fmap;


void register开发者_如何学C_hash_names()
{
    register_function(sha1);
    ...
}

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

Shared.cpp where the error originates from:

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


The definition in the .cpp should be:

function_map_t Config::fmap;
0

精彩评论

暂无评论...
验证码 换一张
取 消