Reputation: 920
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"
Upvotes: 1
Views: 1898
Reputation: 283614
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.
Upvotes: 1