I was learning some fundamentals of c++ and I created a class in "DoubleList.h" and included it in the main.cpp but the terminal just gave me the linking error lnk2019 so I read through all the questions asked in StackOverflow and I guess it is my linker caused the error but unfortunately, I wasn't able to understand and solve so is there any solution to deal with the error
(the error disappears once I include DoubleList.cpp)
what should I do to only included DoubleList.h file but still be able to use the definition in the DoubleList.cpp
DoubleLink.h
#define DOUBLELIST_H
template <class T>
class DoubleList
{
private:
struct Node
{
T value;
Node* next;
Node* previous;
};
Node* head, end;
public:
DoubleList();
~DoubleList();
void appendNode(T);
void showList();
};
#endif // !OUBLELIST_H
DoubleList.cpp
#include <iostream>
using namespace std;
template <class T>
DoubleList<T>::DoubleList()
{
head = nullptr;
end = nullptr;
}
template <class T>
DoubleList<T>::~DoubleList()
{
Node* nodeptr;
for (nodeptr = head->next; nodeptr != end; nodeptr = nodeptr->next)
{
delete(nodeptr->previous);
}
}
template <class T>
void DoubleList<T>::appendNode(T data)
{
Node* newNode = new Node;
newNode->value = data;
if (head == nullptr)
{
newNode->previous = nullptr;
newNode->next = nullptr;
head = newNode;
end = newNode;
}
else
{
newNode->next = nullptr;
end->next = newNode;
newNode->previous = end;
end = newNode;
}
}
template <class T>
void DoubleList<T>::showList()
{
Node* nodeptr;
for (nodeptr = head->next; nodeptr != end; nodeptr = nodeptr->next)
{
cout << nodeptr->value << ' ';
}
cout << endl;
}
main.cpp
#include "DoubleList.h"
#include<iostream>
using namespace std;
int main()
{
DoubleList<int> d_list;
d_list.appendNode(5);
d_list.appendNode(15);
d_list.appendNode(25);
d_list.appendNode(35);
d_list.appendNode(45);
cout << "Content of doubly linked list : " << endl;
d_list.showList();
return 0;
}
This is my project linker configuration Linker General