2013-09-21 61 views
2

我得到以下的错误消息(有人随意编辑不必要比特):C++“致命错误LNK1120”未解决的静态类成员

1> FIXDecoder.obj:错误LNK2001:解析的外部符号“私人: 静态类std :: unordered_map,类std :: allocator>,类 std :: basic_string,类 std :: allocator>,struct std :: hash,类 std :: allocator >>,struct std :: equal_to,class std :: allocator>>,class std :: allocator,class std :: allocator> const,class std :: basic_string,class std :: allocator >> >>> FD :: Fi xValueMappingsDict“ (?FixValueMappingsDict @ FD @@ 0V?$ unordered_map @ V $ $ basic_string @ DU?$ char_traits @ D @ std @@ V $ $ allocator @ D @ 2 @@ std @@ V12 @ U $ $ hash @ V'$ @的basic_string杜?$ @ char_traits @ d @@性病V'$ @分配器@ d @@ 2 STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string杜?$ @ char_traits @ d @性病@V?$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ U&$对@ $$ CBV?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ 2 @@ std @@ V12 @@ std @@@ 2 @@ std @@ A)

1> FD.obj:error LNK2001:无法解析的外部符号“private:static class std :: unordered_map,class std :: allocator>类 std :: basic_string,类 std :: allocator>,std :: hash,类 std :: allocator>>,struct std :: equal_to,class std :: allocator>> ,类std :: allocator,类 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'$分配器@ d @ @@ 2 STD @@ @ V12 U&$散列@ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ 2 @@ STD @@@ 2 @ U&$ equal_to @ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ú ?$ pair @ $$ CBV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ V12 @@ std @@@ 2 @@ std @@ A)

1> C:\视觉工作室2012 \项目\ FD \ 64 \调试\ FD.exe:致命错误 LNK1120:2周解析的外部

此代码:

//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只是包含创建一个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(); 
. 
. 
}; 

回答

4

您需要在FD.cpp文件来创建静态成员的情况下,一些功能:

//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(){ 
} 

注意你不应该在FD构造函数中初始化它们,因为你可以在构造任何对象之前使用静态成员(并且你需要初始化它们一次而不是每次都是这样)我的对象被构​​造)。

+1

感谢您的!知道这是与静态和翻译单位有关! – user997112

3

你应该实现在FD.cpp文件这两个成员:

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

这样的:

unordered_map<string,string> FD::FIXFieldNoDict; 
unordered_map<string,string> FD::FixValueMappingsDict; 
相关问题