我正在爲我創建的某個類獲取鏈接器錯誤。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「
使用* generic *關鍵字來獲取不需要.h文件的模板類泛型類。 C++版本(* template *關鍵字)沒有外部鏈接,.NET版本(* generic *關鍵字)沒有。 –
@HansPassant:泛型只是表面上類似於模板。他們比他們更像不同,國際海事組織。 –