簡體   English   中英

LNK2019在某些類函數上,但不在其他類函數上(dll中的模板類)

[英]LNK2019 on some class functions, but not on others (template class in dll)

好的,不包括整個代碼庫...

#ifdef KIT_EXPORTS
    #define KIT_API __declspec(dllexport)
    #define EXP_TEMPLATE
#else
    #define KIT_API __declspec(dllimport)
    #define EXP_TEMPLATE extern
#endif


#ifndef KIT_LINKED_LIST_H
#define KIT_LINKED_LIST_H

#includes ...

namespace Kit
{
template <class tmplt>
class KIT_API KitLinkedList
{
private:
    ...

public:
    KitLinkedList()
    {
        ...
    }

    KitLinkedList(tmplt obj)
    {
        ...
    }

    KitLinkedList(const KitLinkedList& other)
    {
        ...
    }

    ~KitLinkedList()
    {
        ...
    }

    void PushBack(tmplt obj)
    {
        KitLinkedListNode<tmplt>* addedNode = new KitLinkedListNode<tmplt>(obj);
        tail->nextNode = addedNode;
        tail = addedNode;
        count++;
    }

    uint64_t Count()
    {
        return count;
    }

    KitLinkedListIterator<tmplt> GetIterator()
    {
        return KitLinkedListIterator<tmplt>(root->nextNode);
    }

... some other happy functions live here

};
}

我的非dll代碼:

KitLinkedList<KitString> savedGameList = saveSet.ListSavedGames();
savedGameList.PushBack(KitString("blah"));

if (savedGameList.Count() > 0)
{
}
  1. 我有一個鏈接列表模板類,它完全在dll中的.h文件中聲明和定義。
  2. 我成功使用了dll之外的模板類,進行了編譯,鏈接和運行
  3. 在類中使用某些函數會導致鏈接器錯誤。

savedGameList.Count()會導致LNK2019,但pushback()和getiterator()不會。

正確的答案是模板類不依賴於dll,因為它所需的所有內容都在標頭中。 因此,應刪除KIT_API。

好的,這使我得以構建,但是我不知道它為什么起作用:

將函數聲明為:

template <class tmplt>
uint64_t Count()
{
    return count;
}

並這樣稱呼:

if (savedGameList.Count<KitString>() > 0)

顯然,編譯器看不到該函數,因為它不攜帶模板化類型(即使它在類聲明的范圍之內)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM