C++ “fatal error LNK1120” unresolved static class members

谁说胖子不能爱 提交于 2019-11-27 19:37:00

问题


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;


来源:https://stackoverflow.com/questions/18934122/c-fatal-error-lnk1120-unresolved-static-class-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!