Class Template trong C++

Chào mọi người, mọi người có thể xem bài này lỗi ở đâu không ạ?

#include "stdafx.h"
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <windowsx.h>
#include <tchar.h>
using namespace std;
template <class T> class Node
{
	
private:
	T1 key;
	Node<T>* next;
public:
	Node(T k)
	{
		key = k;
		next = NULL;
	}
	~Node() {};
	friend class List;
};
template <class T> class List
{
	Node<T>* first;
public:
	
	List()
	{
		first = NULL;
	}
	~List() {}

	void PrintList();
	void InsertFirst(T k);
	void InsertLast(T k);
	bool DeleteNode(T k);
	//Chen y vao sau x
	void InsertAfter(T x, T y);
	//Kiem tra co trong danh sach khong
	bool search(T x);
	void nhapds(List<T> &l);
};
template <class T> void List<T>::nhapds(List<T> &l)
{
	    int n;
		cout << "\nnhap so ptu ban dau cua List: "; 
		cin >> n;
		for (int i = 1; i <= n; i++) 
			InsertFirst(i);
		cout << endl;
};

template <class T> void List <T>::PrintList()
{
	for (Node*p = first; p !=  NULL; p = p->next)
	{
		cout << p->key << ' ';
	}
	cout << endl;
};

template <class T> 
void List<T>::InsertFirst(T k)
{
	Node<T>* pNew = new Node(k);
	pNew->next = first;
	first = pNew;
};

template <class T> 
void List<T>::InsertLast(T k)
{
	cout << "\nNhap k = ";
	cin >> k;
	Node* pNew = new Node(k);
	Node* t = first;
	while (t->next != NULL) {
		t = t->next;
	}
	t->next = pNew;
	pNew->next = NULL;
};

template <class T>
bool List<T>::DeleteNode(T k)
{
	cout << "\nNhap k = ";
	cin >> k;
	Node*p = first;
	Node* temp = NULL;
	if (first == NULL || search(k) == false)
	{
		cout << "Khong tim thay Node co gia tri " << k << " trong danh sach\n";
		return false;
	}
	else
	{
		if (p->key == k)
		{
			first = p->next;
			delete p;
		}
		else
		{
			while (p != NULL && p->key != k) 
			{
				temp = p;
				p = p->next;
			}
			temp->next = p->next;
			delete p;
		}
	}
	return true;
};

template <class T>
void List<T>::InsertAfter(T x, T y)
{
	Node* t = first;
	Node* pNew = new Node(y);
	Node* p;
	if (first == NULL || search(x) == false)
		cout << "Khong tim thay Node co gia tri " << x << " trong danh sach\n";
	else {
		while (t != NULL && t->key != x)
		{
			t = t->next;
		}
		p = t->next;
		t->next = pNew;
		pNew->next = p;
	}
};

template <class T>
bool List<T>::search(T x)
{
	for (Node*p = first; p != NULL; p = p->next)
	{
		if (p->key == x)
			return true;
	}
	return false;
};


void main()
{
	List<int> L;
	int k = 0;
    L.nhapds(L);
	cout << "Danh sach:\n";
	L.PrintList();
	cout << endl;
	cout << "Them vao cuoi danh sach cac gia tri:\n";
	L.InsertLast(k);
	L.PrintList();
	cout << endl;

	cout << "Xoa nut trong danh sach\n";
	L.DeleteNode(k);
	L.PrintList();
	cout << endl;

	cout << "Chen nut 100 sau nut 9\n";
	L.InsertAfter(9, 100);
	L.PrintList();


	List<float> A;
	float x = 0; 
	A.nhapds(A);
	cout << "Danh sach:\n";
	A.PrintList();
	cout << endl;
	cout << "Them vao cuoi danh sach cac gia tri:\n";
	A.InsertLast(k);
	A.PrintList();
	cout << endl;

	cout << "Xoa nut trong danh sach\n";
	A.DeleteNode(x);
	A.PrintList();
	cout << endl;

	cout << "Chen nut 100 sau nut 9\n";
	A.InsertAfter(12, 100);
	A.PrintList();
}

T1 ở đây là cái gì vậy ?

chỗ đó mình viết nhầm thôi
sửa đi thì nó vẫn báo lỗi bạn à

nhưng mình có dùng hàm bạn

mà bạn nói rõ giúp mình ko dùng hàm void main nữa là sao

Không dùng void main() vì đó là yêu cầu bắt buộc. Hãy dùng int main().

Node, Node* mình thêm vào rồi nó vẫn báo lỗi bạn ơi

Code bạn khá khó debug :neutral_face: đợi cao nhân khác vào xem vậy.

dù sao cũng cảm ơn bạn nhiều !!!

  • InsertLast thiếu trường hợp first = NULL

  • Kiểu float không nên so sánh ==.

  • Destructor không giải phóng bộ nhớ heap.

1 Like

thì phải làm sao bạn mình ko hiểu

Kiểu float không nên so sánh ==

Destructor không giải phóng bộ nhớ heap

~List() {
  Node *temp;
  if (first == NULL) return;
  
  while (first != NULL) {
    temp = first;
    first = first->next;
    delete temp;
  }
}
1 Like
template <class T> void List<T> :: nhapds(List<T> &l)
template <class T> void List <T>::PrintList()
void List<T>::InsertFirst(T k)
...

chương trình nó báo lỗi các câu lệnh trên, mình cũng chẳng hiểu sao

Thấy dấu ; là lạ không? :kissing:

template <class T> void List<T>::nhapds(List<T> &l)
{
  //...
};

bỏ dấu ; đi nó vẫn báo lỗi ở những dòng đấy

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