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

发布于 2024-12-09 05:37:14 字数 2178 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-12-16 05:37:14

模板位于头文件中,所有用户都可以看到其中的实现。

您确定要使用模板而不是泛型吗?看起来你所做的事情并没有从专业化中受益。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文