I faced a weird linking problem when trying to compile my source code. I paste my code below for better explanation.
LinkedList.h
#ifndef _LINKED_LIST
#define _LINKED_LIST
#include <iostream>
#include "ListInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class LinkedList : public ListInterface<ItemType>{............
//There is the some code here, but thats not the point so i don't
#include "LinkedList.cpp"
#endif
main.cpp
#include "LinkedList.h"
int main()
{
LinkedList<int> list;
}
You can see that under the LinkedList.h header file, i included this line #include "LinkedList.cpp
at the bottom.
So now i can compile like this:
g++ main.cpp -o main
. This give me no problem at all the program works correctly.
But the linking problem appear when i remove this line #include "LinkedList.cpp
at the bottom of LinkedList.h header file. And i compile like this:
g++ main.cpp LinkedList.cpp -o main
. Theoretically this should not be a problem, i done this most of the time with other projects. So this problem is kind weird for me. Can anyone pointing out what is the cause of this?