NapkinTrout
NapkinTrout

Reputation: 269

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

Ok, without including the whole codebase...

#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

};
}

My non-dll code:

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

if (savedGameList.Count() > 0)
{
}
  1. I have a linked list template class declared and defined entirely in a .h file, inside a dll.
  2. I successfully use the template class outside of the dll, compiling, linking, and running
  3. Using some functions in the class cause a linker error.

savedGameList.Count() causes LNK2019, but the pushback() and getiterator() don't.

Upvotes: 0

Views: 90

Answers (2)

NapkinTrout
NapkinTrout

Reputation: 269

The correct answer is that the template class isn't dependent on the dll, since everything it needs is in the header. So KIT_API should be removed.

Upvotes: 1

NapkinTrout
NapkinTrout

Reputation: 269

Ok, this got me building, but I don't know why it works:

Declare the function as:

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

And call it thus:

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

Apparently the compiler doesn't see the function because it doesn't carry the templated type (even though it is within the scope of the class declaration)

Upvotes: 0

Related Questions