Code bị lỗi [Error] no match for ‘operator<<’

Em chỉ cout bình thường thôi , em cũng không biết sao sai ạ. Mong mn giúp đỡ , em cảm ơn
[Error] no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘DiemThi’)

#include <iostream>
#include <string.h>
#include <fstream>
const int MAXLOP = 500 ;
const int MAXCAUHOI=2000 ;
const int MAXDATHI=100 ;
using namespace std ;
//==================CAU HOI DA THI=============================


struct CauHoiDaThi{
	int id ; 	
	int DAchon;
	//----------Phuong thuc-----------
	void Read_CHDT(ifstream &file) ;
	void Write_CHDT(ofstream &file) ;
};
struct DanhSachCHDT{
	int sldt = 0 ;
	CauHoiDaThi *NodesCHDT[MAXDATHI] ;
	//---------Phuong thuc-------------
	//void Nhap_CHDT(int );
	int Delete_DS_CHDT();
	void Insert_CHDT(int limit , int arr[], int ans[] ) ;
	void Read_DS_CHDT(ifstream &file) ;
	void Write_DS_CHDT(ofstream &file) ;
};


//

struct DiemThi{ 
	string MaMonHoc ;
	float Diem ;
	DanhSachCHDT CHDT ;

	//======================PHUONG THUC=====================
	
	
	void Xuat_DT(int x , int y ) ;
	void Read_DT(ifstream &file);
	void Write_DT(ofstream &file);
};
struct NodeDiemThi{
	DiemThi DT;
	NodeDiemThi *DTnext;
};
struct DanhSachDiemThi{
	int SlDT=0 ;
	NodeDiemThi *First = NULL ;
	NodeDiemThi *Last = NULL ;
	//===================PHUONG THUC====================
	
	
	void InsertLast_DT(DiemThi a);
	void Insert_DT(string id , float diem , int arr[], int limit) ;
	void Xuat_DT(int x, int y , string mamonhoc) ;
	DiemThi *Search_DT(string mamonhoc) ;
	void Delete_DS_DT();
	void Read_DS_DT(ifstream &file);
	void Write_DS_DT(ofstream &file);
};



int main(){
	DanhSachCHDT listCHDT  ;
	int arr[MAXCAUHOI];
	int ans[MAXCAUHOI];
	
	DanhSachDiemThi listDT  ;
	DiemThi a ;
	a.Diem = 5;
	listDT.InsertLast_DT(a) ;
	cout<<"Danh sach diem thi\n";
	NodeDiemThi *p = listDT.First ;
	while(p!=NULL){
		cout << p->DT << endl  ; // Máy báo lỗi chổ này ạ 
		p=p->DTnext ;
	}
	
	
}

void CauHoiDaThi::Read_CHDT(ifstream &file){
	file >> id ;
	file >> DAchon;
}
void CauHoiDaThi::Write_CHDT(ofstream &file){
	file << id <<"   "<<DAchon<<'\n';
}
//=============PHUONG THUC DS CHDT===============
int DanhSachCHDT::Delete_DS_CHDT(){
	for(int i=0 ; i<sldt ; i++){
		delete NodesCHDT[i] ;
	}
	sldt=0;
}
void DanhSachCHDT::Insert_CHDT(int limit , int arr[], int ans[] ){
	sldt=limit ;
	cout<<sldt ;
	for(int i=0 ; i<limit ; i++){
		NodesCHDT[i] = new CauHoiDaThi ;
		NodesCHDT[i]->id = arr[i] ;
		NodesCHDT[i]->DAchon=ans[i]; 
	}
}
void DanhSachCHDT::Read_DS_CHDT(ifstream &file){
	file >> sldt  ;
	for(int i=0 ; i<sldt ; i++){
		NodesCHDT[i] = new CauHoiDaThi ;
		NodesCHDT[i]->Read_CHDT(file) ;
	}
}
void DanhSachCHDT::Write_DS_CHDT(ofstream &file){
	file << sldt <<endl ;
	for(int i=0 ; i<sldt ; i++){
		NodesCHDT[i]->Write_CHDT(file) ;
	}
}
//=============PHUONG THUC DANH SACH DIEM THI===
void DanhSachDiemThi::InsertLast_DT(DiemThi a){
	NodeDiemThi *pNode = new NodeDiemThi ;
	pNode->DT=a ;
	pNode->DTnext=NULL;
	if(First==Last==NULL){
		First=Last=pNode ;
	}
	else{
		Last->DTnext=pNode ;
		Last=pNode ;
	}
	SlDT++;
}

Bình thường chỗ nào? Mặc định, ostream có chỗ nào nhận kiểu DiemThi của bạn làm tham số cho toán tử << không? Xem đặc tả nhé: http://www.cplusplus.com/reference/ostream/ostream/operator<</
Vì thế mà trong lớp DiemThi có phương thức DiemThi::Write_DT(ostream& file). Nhưng chẳng hiểu sao, bạn khai báo nó mà không định nghĩa và cũng chẳng dùng đến?!

  1. Đã khai báo rồi, giờ định nghĩa và dùng thôi.
  2. Dùng phương thức bạn bè (friend). Từ khóa: c++ friend operator.
1 Like

Dạ em cảm ơn , em hiểu rồi ạ!

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