C++/CLI 中未解析的令牌
我创建的类之一出现链接器错误。
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
这些是我尝试使用该类的所有地方。这是该类的代码:
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);
};
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);
}
}
在我尝试使用它的地方,我只是做 #include "TwoKeyDict.h"
I'm getting linker errors for one of the classes I created.
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
These are in all the places that I try to use the class. Here's the code for the class:
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);
};
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);
}
}
In the places that I'm trying to use it I'm just doing #include "TwoKeyDict.h"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模板位于头文件中,所有用户都可以看到其中的实现。
您确定要使用模板而不是泛型吗?看起来你所做的事情并没有从专业化中受益。
Templates go in header files, where the implementation can be seen by all users.
Are you sure you wanted to use a template and not a generic? It doesn't look like you're doing anything that would benefit from specialization.