C++ “fatal error LNK1120” unresolved static class

2020-02-07 05:47发布

问题:

I get the following error message (someone feel free to edit the unnecessary bits):

1>FIXDecoder.obj : error LNK2001: unresolved external symbol "private: static class std::unordered_map,class std::allocator >,class std::basic_string,class std::allocator >,struct std::hash,class std::allocator > >,struct std::equal_to,class std::allocator > >,class std::allocator,class std::allocator > const ,class std::basic_string,class std::allocator > > > > FD::FixValueMappingsDict" (?FixValueMappingsDict@FD@@0V?$unordered_map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@@std@@A)

1>FD.obj : error LNK2001: unresolved external symbol "private: static class std::unordered_map,class std::allocator >,class std::basic_string,class std::allocator >,struct std::hash,class std::allocator > >,struct std::equal_to,class std::allocator > >,class std::allocator,class std::allocator > const ,class std::basic_string,class std::allocator > > > > FD::FIXFieldNoDict" (?FIXFieldNoDict@FD@@0V?$unordered_map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@@std@@A)

1>C:\visual studio 2012\Projects\FD\x64\Debug\FD.exe : fatal error LNK1120: 2 unresolved externals

for this code:

//FH.h
#ifndef FD_H
#define FD_H

#include "FM.h"
#include <unordered_map>
#include <string>

class FD{
public:
    FD();
    FD(FM message);
    ~FD();
    FD(const FD& tocopy);
    FD& operator=(const FD& toassign);

private:
    static unordered_map<string,string> FIXFieldNoDict;
    static unordered_map<string,string> FixValueMappingsDict;
};

#endif

//FD.cpp
#include "FD.h"
#include "Mappings.h"
#include "Utils.h"
#include <vector>
#include <string>
#include <iostream>
#include <unordered_map>

using namespace std;

FD::FD(){
    FIXFieldNoDict = Mappings::createFIXFieldNoDict();
    FixValueMappingsDict = Mappings::getFIXValuesDict();
}

Mappings.h just contains some functions which create an unordered_map

#ifndef MAPPINGS_H
#define MAPPINGS_H

#include <unordered_map>
#include <string>

using namespace std;

class Mappings{

public:
    Mappings();

    static unordered_map<string,string> createFIXFieldNoDict();

    static unordered_map<string,string> getFIXValuesDict();
.
.
};

回答1:

You need to create instances of your static members in the FD.cpp file:

//FD.cpp
#include "FD.h"
#include "Mappings.h"
#include "Utils.h"
#include <vector>
#include <string>
#include <iostream>
#include <unordered_map>

using namespace std;

unordered_map<string,string> FD::FIXFieldNoDict = Mappings::createFIXFieldNoDict();
unordered_map<string,string> FD::FixValueMappingsDict = Mappings::getFIXValuesDict();


FD::FD(){
}

Note that you shouldn't initialize them in the FD constructor, as you can use static members before construction of any object (and you need to initialize them once and not every time when some object is constructed).



回答2:

You should implement this two members in FD.cpp file:

static unordered_map<string,string> FIXFieldNoDict;
static unordered_map<string,string> FixValueMappingsDict;

like that:

unordered_map<string,string> FD::FIXFieldNoDict;
unordered_map<string,string> FD::FixValueMappingsDict;