C ++“致命错误LNK1120”未解析的静态类成员 [英] C++ "fatal error LNK1120" unresolved static class members

查看:249
本文介绍了C ++“致命错误LNK1120”未解析的静态类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误消息(有人随意编辑不必要的位):


1> FIXDecoder.obj:error LNK2001 :未解析的外部符号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> 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 @ @ @ std @@@ 2 @ V $ $ allocator @ U?$ pair @ $$ CBV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ @ std @@ V12 @@ std @@@@@@ std @@ A >

1> FD.obj:error LNK2001:未解析的外部符号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 @ @ @ 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:致命错误
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
#includeFD.h
#includeMappings.h
# includeUtils.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();


};


解决方案

您需要创建静态成员的实例FD.cpp文件:

  // FD.cpp 
#includeFD.h
# includeMappings.h
#includeUtils.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 构造函数中初始化它们,因为您可以在构造任何对象之前使用静态成员(并且您需要初始化它们一次,而不是每次构建一些对象时)。


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();
.
.
};

解决方案

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

这篇关于C ++“致命错误LNK1120”未解析的静态类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆