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?