2

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();
.
.
};
Nemanja Boric
22.2k6 gold badges72 silver badges93 bronze badges
asked Sep 21, 2013 at 15:03
0

2 Answers 2

4

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).

answered Sep 21, 2013 at 15:08
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that! Knew it was something to do with static and translation units!
3

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;
answered Sep 21, 2013 at 15:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.