C ++: Возникают проблемы с использованием карты со статической переменной в классе, я получаю неопределенный символ - PullRequest
1 голос
/ 11 мая 2011

Это ошибка, которую я получаю

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_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";

1 Ответ

7 голосов
/ 11 мая 2011

Определение в .cpp должно быть:

function_map_t Config::fmap;
...