Lỗi file .h không thể gọi file .cpp khi sử dụng teamplate

Mình có file .h và file .cpp khi không dùng teamplate thì không bị lỗi gì hết nhưng khi dùng teamplate thì file .h không gọi được file .cpp.Copy toàn bộ file .cpp qua file .h thì không còn bị lỗi nữa

file .h

#pragma once
#include "Node.h"

template <class L>
class LinkedList
{
private:
Node<L> *first;
public:
int length;
LinkedList();
~LinkedList();
};

file .cpp

#include "LinkedList.h"

template<class L>
LinkedList<L>::LinkedList()
{
	length = 0;
}

template <class L>
LinkedList<L>::~LinkedList()
{
}

lỗi

Error	error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::LinkedList<int>    (void)" (??0?$LinkedList@H@@QAE@XZ) referenced in function _main	CTDL_LinkedList	main.obj	

Error	error LNK2019: unresolved external symbol "public: __thiscall LinkedList<int>::~LinkedList<int>(void)" (??1?$LinkedList@H@@QAE@XZ) referenced in function _main	CTDL_LinkedList	main.obj

mong mọi người giúp đỡ

2 Likes
1 Like

@NuKan_Tran lý do là em không thể khai báo template trên file .CPP. Em chỉ có thể khai báo trên file .H thôi. Anh cũng không để ý cái này luôn, vì lúc nào anh cũng khai báo trên file .H

The only portable way of using templates at the moment is to implement them in header files by using inline functions.

@Is2IT anh thấy link này hợp lý hơn

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?