Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio C+中的未解析令牌+/CLI_Visual Studio_Visual Studio 2010_Visual C++_C++ Cli_Managed C++ - Fatal编程技术网

Visual studio C+中的未解析令牌+/CLI

Visual studio C+中的未解析令牌+/CLI,visual-studio,visual-studio-2010,visual-c++,c++-cli,managed-c++,Visual Studio,Visual Studio 2010,Visual C++,C++ Cli,Managed C++,我创建的一个类出现链接器错误 1>Dict.obj : error LNK2020: unresolved token (06000006) TwoKeyDict<System::String ^,int>::.ctor 1>Dict.obj : error LNK2020: unresolved token (06000007) TwoKeyDict<System::String ^,int>::Get 1>Dict.obj : error LNK20

我创建的一个类出现链接器错误

1>Dict.obj : error LNK2020: unresolved token (06000006) TwoKeyDict<System::String ^,int>::.ctor
1>Dict.obj : error LNK2020: unresolved token (06000007) TwoKeyDict<System::String ^,int>::Get
1>Dict.obj : error LNK2020: unresolved token (06000008) TwoKeyDict<System::String ^,int>::Put
1>Dict.obj : error LNK2020: unresolved token (06000009) TwoKeyDict<int,int>::.ctor
1>Dict.obj : error LNK2020: unresolved token (0600000A) TwoKeyDict<int,int>::Get
1>Dict.obj : error LNK2020: unresolved token (0600000B) TwoKeyDict<int,int>::Put
1>Dict.obj:错误LNK2020:未解析令牌(06000006)TwoKeyDict::.ctor
1> Dict.obj:错误LNK2020:未解析的令牌(06000007)TwoKeyDict::Get
1> Dict.obj:错误LNK2020:未解析令牌(06000008)TwoKeyDict::Put
1> Dict.obj:错误LNK2020:未解析令牌(06000009)TwoKeyDict::.ctor
1> Dict.obj:错误LNK2020:未解析的令牌(0600000A)TwoKeyDict::Get
1> Dict.obj:错误LNK2020:未解析的令牌(0600000B)TwoKeyDict::Put
这些都是我尝试使用这个类的所有地方。下面是该类的代码:

TwoKeyDict.h

#pragma once

using namespace System::Collections::Generic;

template<class K, class V>
public ref class TwoKeyDict
{
private:
    Dictionary<K, Dictionary<K, V>^>^ underlyingDict;
public:
    TwoKeyDict();
    V Get(K key1, K key2);
    void Put(K key1, K key2, V value);
};
#pragma一次
使用命名空间System::Collections::Generic;
模板
公共引用类TwoKeyDict
{
私人:
词典;词典;
公众:
TwoKeyDict();
V Get(K键1,K键2);
无效看跌期权(K键1、K键2、V值);
};
TwoKeyDict.cpp

#include "StdAfx.h"
#include "TwoKeyDict.h"

template<class K, class V>
TwoKeyDict<K, V>::TwoKeyDict() {
    underlyingDict = gcnew Dictionary<K, Dictionary<K, V>^>();
}

template<class K, class V>
V TwoKeyDict<K, V>::Get(K key1, K key2) {
    if (underlyingDict->ContainsKey(key1)) {
        if (underlyingDict[key1]->ContainsKey(key2)) {
            return underlyingDict[key1][key2];
        }
    }

    return nullptr;
}

template<class K, class V>
void TwoKeyDict<K, V>::Put(K key1, K key2, V value) {
    if (underlyingDict->ContainsKey(key1)) {
        Dictionary<K, V>^>^ secondLayerDict = underlyingDict[key1];
        if (secondLayerDict->ContainsKey(key2)) {
            secondLayerDict[key2] = value;
        } else {
            secondLayerDict->Add(key2, value);
        }
    } else {
        Dictionary<K, V>^>^ secondLayerDict = gcnew Dictionary<K, V>^>();
        secondLayerDict->Add(key2, value);
        underlyingDict->Add(key1, secondLayerDict);
    }
}
#包括“StdAfx.h”
#包括“TwoKeyDict.h”
模板
TwoKeyDict::TwoKeyDict(){
underyingdict=gcnewdictionary();
}
模板
V TwoKeyDict::Get(kkey1,kkey2){
if(参考目录->容器(键1)){
if(underlyingDict[key1]->ContainsKey(key2)){
返回underyingdict[key1][key2];
}
}
返回空ptr;
}
模板
void TwoKeyDict::Put(K键1、K键2、V值){
if(参考目录->容器(键1)){
字典^>^secondLayerDict=underyingDict[key1];
如果(第二层数据->容器(键2)){
secondLayerDict[key2]=值;
}否则{
第二层编辑->添加(键2,值);
}
}否则{
字典^>^SecondLayerDictionary=gcnew Dictionary^>();
第二层编辑->添加(键2,值);
基础目录->添加(第二层目录,键1);
}
}

在我试图使用它的地方,我只是在做一些事情,包括“TwoKeyDict.h”

模板放在头文件中,所有用户都可以看到它的实现


您确定要使用模板而不是通用模板吗?看起来您没有做任何可以从专业化中获益的事情。

模板放在头文件中,所有用户都可以在头文件中看到实现


您确定要使用模板而不是通用模板吗?看起来您并没有做任何可以从专门化中获益的事情。

使用generic关键字来获取不需要.h文件的类似模板的泛型类。C++版本(模板关键字)没有外部链接,.NET版本(通用关键字)。@ HANSPASANT:泛型只是表面上类似于模板。依我看,它们的区别比它们的相似之处要大得多。使用generic关键字可以获得不需要.h文件的类似模板的泛型类。C++版本(模板关键字)没有外部链接,.NET版本(通用关键字)。@ HANSPASANT:泛型只是表面上类似于模板。在我看来,它们比它们相似之处更为不同。我认为我根本不需要一个模板。当我决定使用哪一种时,我读到的一些东西似乎暗示泛型不适用于原语,但事实似乎并非如此。是这样吗?@Alex:你想到的是Java,原语被装箱;在.NET中,泛型与原语一样100%正常工作。我认为我根本不需要模板。当我决定使用哪一种时,我读到的一些东西似乎暗示泛型不适用于原语,但事实似乎并非如此。是这样吗?@Alex:你想到的是Java,原语被装箱;在.NET中,泛型与原语一样100%正常工作。