1

I don't know why: When declarations and definitions are not separated from the block's class(class SLinkedList{ ....}). The building will be successful.

But when I separate declarations and definitions of the class below. The building will report an error like this

"Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall SLinkList<char>::SLinkList<char>(void)" (??0?$SLinkList@D@@QAE@XZ) referenced in function _main LinkedLists C:\Users\LQA\Documents\Visual Studio 2015\Projects\DataStructures\DataStructures\Source.obj 1"

This is the detail source code: [LinkedLists.h]

#include <iostream>
#include <string>
using namespace std;


//Singly Linked List

template<typename E>
class SLinkList;

template<typename E>
class SNode
{
private:
    E elem;
    SNode<E>* next;
    friend class SLinkList<E>;
};

template<typename E>
class SLinkList
{
public:
    SLinkList();
    ~SLinkList();
    bool empty() const;
    const E& front() const;
    void addFront(const E& e);
    void removeFront();
private:
    SNode<E>* head;
};

[LinkedLists.cpp]

#include "LinkedLists.h"


/*
 * SINGLY LINKED LIST
 */
template<typename E>
SLinkList<E>::SLinkList() :
    head(NULL) {}

template<typename E>
SLinkList<E>::~SLinkList()
{
    while (!this->empty())
    {
        removeFront();
    }
}

template<typename E>
bool SLinkList<E>::empty() const
{
    return head == NULL;
}

template<typename E>
const E& SLinkList<E>::front() const
{
    return this->head->elem;
}

template<typename E>
void SLinkList<E>::removeFront()
{
    SNode<E>* old = this->head;
    this->head = old->next;
    delete old;
}

template<typename E>
void SLinkList<E>::addFront(const E& e)
{
    SNode<E>* v = new SNode<E>;
    v->elem = e;
    v->next = this->head;
    this->head = v;
}

[Source.cpp]

#include "LinkedLists.h"
using namespace std;

int main(void)
{
    SLinkList<char>* llst = new SLinkList<char>();
    //llst->addFront('a');
    //llst->addFront('b');
    return 0;
}

Does anyone know what was caused to this problem?

acraig5075
  • 10,588
  • 3
  • 31
  • 50
LPA1HC
  • 11
  • 2

2 Answers2

0

Did you try to add "__declspec(dllexport)" before the class declaration in h file?

As soon as you move class function members definition to separate cpp file, then you need to add this specifier in order to make all that definitions visible outside the dll.

  • I had tried to add __declspec(dllexport) macro. The VS 2015 had generated a *.dll file. But when I used another project and linked to the *.dll, It still reported that error. If you can make it to build successfully. Can you share me how to make it. – LPA1HC Aug 29 '18 at 02:24
  • You can try to inspect the dll with tool Dependency Walker, to actually see if those functions are exported or not. – dusan.stanivukovic Aug 29 '18 at 19:34
0

If you declare template class in c++, you should include function body to header file.

for example: [LinkedLists.h]

...
template<typename E>
class SLinkList
{
public:
    SLinkList() : head(NULL) {};
    ~SLinkList() 
    {
        while (!this->empty())
        {
            removeFront();
        }
    };

    bool empty() const
    {
        return head == NULL;
    }
...