Hỏi về lỗi "undefined reference to" trong DevC

Cho em hỏi cách fix lỗi này với ạ? Em có gg rồi nhưng chưa có fix được.

Full code ở đây ạ
https://onlinegdb.com/tfATAuOd2

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<iomanip>

using namespace std;

//Class Person
class Person {
private:
	string name;
	int age;
public:
	////Constructor with parameter declaration
	Person(string name, int age) {
		this->name = name;
		this->age = age;
	}
	
	// Setter and Getter
	string getName() {
		return name;
	}		
	void setName(string name) {
		this->name = name;
	}
	
	int getAge() {
		return age;
	}
	void setAge(int age) {
		this->age = age;
	}
};

// Class Book
class Book {
private:
	int bookId;
	string name;
	string author;
	int amount;
public:
	//Constructor declaration
	Book();
	//Constructor with parameter declaration
	Book(int bookId, string name, string author, int amount) {
		this->bookId = bookId;
		this->name = name;
		this->author = author;
		this->amount = amount;
	}
	
	//Setter and Getter
	int getBookId() {
		return bookId;
	}
	void setBookId(int bookId) {
		this->bookId = bookId;
	}
	string getName() {
		return name;
	}
	void setName(string name) {
		this->name = name;
	}
	string getAuthor() {
		return author;
	}
	void setAuthor(string author) {
		this->author = author;
	}
	int getAmount() {
		return amount;
	}
	void setAmount(int amount) {
		this->amount = amount;
	}
	
	
	
	//Properties
	void addBook() {
		cout << "NEW BOOK ENTRY" << endl;
		cout << "Please enter the information of your new book: " << endl;
		cout << "ID: ";
		cin >> bookId;
		cout << "Name: ";
		cin >> name;
		cout << "Author: ";
		cin >> author;
		cout << "Amount of books in the storage: ";
		cin >> amount;
		cout << "A new book is successfully added." << endl;
	}
	
	void showBook() {
		cout << "INFORMATION:" << endl;
		cout << "ID:     " << bookId << endl;
		cout << "Name:   " << name << endl;
		cout << "Author: " << author << endl;
		cout << amount << " books left." << endl;
	}
	
	void changeBook() {
		cout << "MODIFICATION:" << endl;
		cout << "Please enter the new information of the book " << bookId << " ." << endl;
		cout << "Name: ";
		cin >> name;
		cout << "Author: ";
		cin >> author;
		cout << "Amount of books in the storage: ";
		cin >> amount;
		cout << "This book is successfully modified." << endl;
	}
	void display() {
		cout << setw(30) << bookId << setw(30) << name << setw(30) << author << setw(30) << amount << endl;
	}
};

using namespace std;

//Class Student
class Student:public Person {
private:
	int reNum;
	int stuId;
	int bBorrow;
public:
	//Constructor
	Student();
	
	//Constructor with parameter declaration
	Student(int reNum, string name, int age, int stuId,int bBorrow):Person(name, age) {
		this->reNum = reNum;
		this->stuId = stuId;
		this->bBorrow = bBorrow;
	}
	
	// Setter and Getter
	int getReNum() {
		return reNum;
	}
	void setReNum(int reNum) {
		this->reNum = reNum;
	}		
	
	int getStuId() {
		return stuId;
	}
	void setStuId(int stuId) {
		this->stuId = stuId;
	}
	
	int getBBorrow() {
		return bBorrow;
	}
	void setBBorrow(int bBorrow) {
		this->bBorrow = bBorrow;
	}
	void addBBorrow() {
		bBorrow = 1;
	}
	
	//Add a new student
	void addStudent() {
		cout << "NEW STUDENT REGISTRATION" << endl;
		cout << "Please enter your information:" << endl;
		cout << "Registration number: ";
		cin >> reNum;
		cout << "Full Name: ";
		Person::getName();
		cout << "Age: ";
		Person::getAge();
		cout << "Student's ID: ";
		cin >> stuId;
		cout << "Registration Completed." << endl;
		cout << "The password is your Student's ID." << endl;
	}
	
	void showStudent() {
		cout << "INFORMATION:" << endl;
		cout << "Registration number:   " << reNum << endl;
		cout << "Full Name:             " << Person::getName() << endl;
		cout << "Age:                   " << Person::getAge() << endl;
		cout << "Student's ID:          " << stuId << endl;
		if (bBorrow == 1 ) {
			cout << "Book borrowed: Yes" << endl;
			 
		}else {
			cout << "Book borrowed: No" << endl;
		}
	}
	
	void changeStudent() {
		cout << "MODIFICATION:" << endl;
		cout << "Please enter the new information of the student with ID " << stuId << " ." << endl;
		cout << "Registration number: ";
		cin >> reNum;
		cout << "Full Name: ";
		Person::getName();
		cout << "Age: ";
		Person::getAge();
		
	}
};


//Class Teacher
class Teacher:public Person {
private:
	string department;
	string email;
public:
	//Constructor
	Teacher();
	//Constructor with parameter declaration
	Teacher(string name, int age, string department, string email):Person(name,age) {
		this->department = department;
		this->email = email;
	}
	// Setter and Getter		
	string getDepartment() {
		return department;
	}
	void setDepartment(string department) {
		this->department = department;
	}
	
	string getEmail() {
		return email;
	}
	void setEmail(string email) {
		this->email = email;
	}
	
	//Add a new teacher
	void addTeacher() {
		cout << "NEW TEACHER REGISTRATION" << endl;
		cout << "Please enter your information:" << endl;
		cout << "Name: ";
		Person::getName();
		cout << "Age: ";
		Person::getAge();
		cout << "Department: ";
		cin >> department;
		cout << "Email: ";
		cin >> email;
		cout << "Registration Completed." << endl;
		cout << "Your password: hustteacher" << endl; 
	}
	void showTeacher() {
		cout << "INFORMATION:" << endl;
		cout << "Full Name:   " << Person::getName() << endl;
		cout << "Age:         " << Person::getAge() << endl;
		cout << "Department:  " << department << endl;
		cout << "Email:       " << email << endl;
	}
};
//Declaration
fstream f,f1;
Book book;
Student stu;
Teacher tea;

//Input Books'Info into file BookInput.txt
void bookInput() {
	int ans1;
	fstream f("BookInput.txt", ios::out | ios::app);
	do {
		system("cls");
		book.addBook();
		f.write((char*)&book,sizeof(book));
		cout << "Do you want to add more books? (1 for Yes, 2 for No)" << endl;
		cin >> ans1;
		
	}while(ans1 == 1);
	f.close();
}

//Input Students'Info into file StudentInput.txt
void studentInput() {
	char ans2;
	fstream f("StudentInput.txt", ios::out | ios::app);
	//do {
		system("cls");
		stu.addStudent();
		f.write((char*)&stu,sizeof(stu));
		//cout << "Do you want to add more students? " << endl;
		//cin >> ans2;
	//}while(ans2 == 'Y' | ans2 == 'y' | ans2 == 'Yes' | ans2 == 'yes');
	f.close();
}

//Input Teachers'Info into file TeacherInput.txt
void teacherInput() {
	fstream f("TeacherInput.txt", ios::out | ios::app);	
	system("cls");
	tea.addTeacher();
	f.write((char*)&tea,sizeof(tea));
	f.close();	
	
}

//Search Book and display
void searchBook(int n) {
	int temp = 0;
	int in;
	string nam;
	//Search by ID
	if(n == 1) {
		cout << "Please enter the ID: ";
		cin >> in;
		system("cls");
		fstream f("BookInput.txt", ios::in);
		while(f.read((char*)&book,sizeof(book))) {
			if(book.getBookId()== in ) {
				book.showBook();
				temp = 1;
			}
		}
		f.close();
		if(temp == 0) {
			cout << "Sorry! This book is not in the library or you have typed the wrong ID." << endl;
		}
	}else if ( n == 2) { // Seach by Book's name
		cout << "Please enter book's name: ";
		cin >> nam;
		fstream f("BookInput.txt", ios::in);
		while(f.read((char*)&book,sizeof(book))) {
			if(book.getName() == nam) {
				
				book.showBook();
				temp = 1;
			}
		}
		f.close();
		if(temp == 0) {
			cout << "Sorry! This book is not in the library or you have typed the wrong name." << endl;
		}
	}else {
		cout << "Please enter correct syntax.";
	}	
}

// Search Student
void searchStudent(int ins) {
	int temp = 0;
	cout << "STUDENT INFORMATION: " << endl;
		fstream f("StudentInput.txt", ios::in);
		while(f.read((char*)&stu,sizeof(stu))) {
			if(stu.getStuId() == ins) {
				stu.showStudent();
				temp = 1;
			}
		}
		f.close();
		if(temp == 0) {
			cout << "Sorry! Student cannot be found." << endl;
		}
}

// Change Book's information
void bookChange() {
	int id;
	int temp = 0;
	system("cls");
	cout << "Please enter the ID of book you want to change: ";
	cin >> id;
	fstream f("BookInput.txt", ios::out | ios::in);
	while(f.read((char*)&book,sizeof(book)) && temp == 0) {
		if(id == book.getBookId()) {
			cout << "CURRENT INFORMATION: " << endl;
			book.showBook();
			book.changeBook();
			f.write((char*)&book,sizeof(book));
			temp = 1;	
		}
	}
	f.close();
	if(temp == 0) {
		cout << "BOOK NOT FOUND." << endl;
	}
}

// Change Student's information
void studentChange() {
	int id;
	int temp = 0;
	system("cls");
	cout << "Please enter the ID of student you want to change: ";
	cin >> id;
	fstream f("StudentInput.txt", ios::out | ios::in);
	while(f.read((char*)&stu,sizeof(stu)) && temp == 0) {
		if(id == stu.getStuId()) {
			cout << "CURRENT INFORMATION: " << endl;
			stu.showStudent();
			stu.changeStudent();
			f.write((char*)&stu,sizeof(stu));
			temp = 1;	
		}
	}
	f.close();
	if(temp == 0) {
		cout << "STUDENT NOT FOUND." << endl;
	}
}

// Display all books
void display() {
	system("cls");
	fstream f("BookInput.txt", ios::in);
	cout << "______________________________________________________________" << endl;
	cout << "__________________________INFORMATION_________________________" << endl;
	cout << setw(30) << "ID" << setw(30) << "NAME" << setw(30) << "AUTHOR" << setw(30) << "AMOUNT" << endl;
	cout << "______________________________________________________________" << endl;
	while(f.read((char*)&book,sizeof(book))) {
		book.display();
	}
	f.close();
}

// Borrow book
void borrow() {
	int i,n;    // ID
	int found = 0,temp = 0;
	string ans;
	string trueA = "Y";
	system("cls");
	cout << "==============BORROW==============" << endl;
	cout << "Enter your ID: ";
	cin >> i ;
	fstream f("StudentInput.txt", ios::in | ios:: out);
	fstream f1("BookInput.txt", ios::in | ios:: out);
	while(f.read((char*)&stu,sizeof(stu)) && found==0) {
		if(i == stu.getStuId()) {
			found = 1;
			if(stu.getBBorrow() == 0) {
				cout << "Enter book ID: ";
				cin >> n;
				while(f1.read((char*)&book,sizeof(book)) && temp==0) {
					if (n == book.getBookId()) {
						book.showBook();
						temp = 1;
						cout << "Do you want to borrow this book? (Y or N)" << endl;
						cin >> ans;
						if (ans == trueA) {
							stu.addBBorrow();
							f.write((char*)&stu,sizeof(stu));
							cout << "Book is borrowed successfully. Please note the day you borrow and return the book to the library within 15 days." << endl;
							cout << "THANK YOU!" << endl;
						}
					}
			  	}
			  	if(temp == 0) {
			  		cout << "Book does not exist." << endl;
				  }
			} else {
				cout << "You havent't returned the previous book!" << endl;
			}
			
		}
	}
	if(found == 0) {
		cout << "STUDENT NOT FOUND." << endl;
	}  
	f.close();
	f1.close();
}


// Student
void student() {
	char x;
	string n;
	int i1,i2,i3;
	int temp = 0;
	system("cls");
	cout << "Are you a new student? (Y or N) " << endl;
	cin >> x;
	if(x == 'Y' || x == 'y') {
		studentInput();
	}
	cout << "LOG IN" <<endl;
	cout << "Full Name: ";
	cin >> n;
	cout << "Password:  ";
	cin >> i1; 
	fstream f("StudentInput.txt", ios::in);	
	while(f.read((char*)&stu,sizeof(stu)) && temp == 0) {
		if(i1 == stu.getStuId() && n == stu.getName() ) {
			system("cls");
			cout << "Login successful." << endl;
			cout << "Welcome " << n << endl;
			cout << "_________________________________" << endl;
			cout << "1. SEARCH A BOOK." << endl;
			cout << "2. DISPLAY ALL BOOKS." << endl;
			cout << "3. BORROW A BOOK." <<endl;
			cout << "4. EXIT." << endl;
			cout << "_________________________________" << endl;
			cout << "Please choose one option: ";
			cin >> i2;
			switch(i2) {
				case 1: system("cls");
						cout << "_________________________________" << endl;
						cout << "Search book by: " << endl;
						cout << "1. ID." << endl;
						cout << "2. NAME." << endl;
						cout << "_________________________________" << endl;
						cin >> i3;
						searchBook(i3);
						break;
				case 2: system("cls");
						display();
						break;
				case 3: borrow();
						break;		
				case 4: exit(0);				
			}
			temp = 1;
		} 
		
	}
	if(temp == 0) {
		student();
	}
}

//Teacher
void teacher() {
	int in,i2,s,i3;
	string n,i1;
	string pass = "hustteacher";
	int temp = 0;
	system("cls");
	cout << "Hello. If this is your first login, please enter 1. If not please enter any number." << endl;
	cin >> in;
	if(in == 1) {
		teacherInput();
	}
	cout << "LOG IN" <<endl;
	cout << "Full Name: ";
	cin >> n;
	cout << "Password:  ";
	cin >> i1; 
	fstream f("TeacherInput.txt", ios::in);	
	while(f.read((char*)&tea,sizeof(tea)) && temp == 0) {
		if(i1 == pass && n == tea.getName() ) {	
			system("cls");
			cout << "Login successful." << endl;
			cout << "Welcome! " <<  endl;
			cout << "_________________________________" << endl;
			cout << "1. SEARCH A BOOK." << endl;
			cout << "2. DISPLAY ALL BOOKS." << endl;
			cout << "3. BORROW A BOOK." <<endl;
			cout << "4. CHANGE A BOOK'S INFORMATION." <<endl;
			cout << "5. INPUT STUDENTS INFORMATION." <<endl;
			cout << "6. SEARCH A STUDENT." <<endl;
			cout << "7. CHANGE A STUDENT'S INFORMATION." <<endl;		
			cout << "8. EXIT." << endl;
			cout << "_________________________________" << endl;
			cout << "Please choose one option: ";
			cin >> i2;
			switch(i2) {
				case 1: system("cls");
						cout << "_________________________________" << endl;
						cout << "Search book by: " << endl;
						cout << "1. ID." << endl;
						cout << "2. NAME." << endl;
						cout << "_________________________________" << endl;
						cin >> i3;
						searchBook(i3);
						break;
				case 2: system("cls");
						display();
						break;
				case 3: borrow();
						break;
				case 4: bookChange();
						break;
				case 5: studentInput();
						break;
				case 6: cout << "Please input the student's ID: ";
						cin >> s;
						searchStudent(s);
						break;
				case 7: studentChange();
						break;
				case 8: exit(0);												 
			}
			temp == 1;
		}
	}
	if(temp == 0) {
		cout << "TEACHER NOT FOUND" << endl; 
	}
}
//Main
int main() {
		int c;
	do {
		system("cls");
		cout << "=============WELCOME=============" << endl;
		cout << "Please choose one option: " << endl;
		cout << "1. STUDENT" << endl;
		cout << "2. TEACHER" << endl;
		cout << "3. EXIT" << endl;
		cin >> c;
		if(c == 1) {
			student();
			break;
		}else if (c == 2) {
			teacher();
			break;
		}else {
			exit(0);
		}
	}while( c != 4);
	return 0;
}

Các hàm dựng không tham số của 3 lớp bạn chỉ mới khai báochưa định nghĩa.
Ví dụ:

public class B: public A{
public:
    A(); // Chỉ mới khai báo.
}

Giải quyết:

public class B: public A{
public:
    A(){} // Khai báo và định nghĩa với nội dung trống.
}
// hoặc
public class B: public A{
public:
    A(); // Khai báo
}
A::A(){} // Định nghĩa
1 Like

Vâng em sửa dc r ạ. Em cảm ơn ạ :slight_smile:

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