thắc mắc Hỏi ngu về pointer C++

MãiYêuVietjet

Senior Member
Chào mấy thím, mấy thím xem giúp em lỗi này với. Đề yêu cầu tạo 1 mảng động để lưu trữ 1 cái danh sách object của student.
Em tạo 1 cái class
1646642241303.png


Em khởi tạo object và cho nó vào cái array
1646642315247.png

Và sau đó đề yêu cầu xóa 1 cái object trong array. Em làm ntn mà ko hiểu sao nó lại sai. Em search mạng hết rồi mà vẫn ko ra:(
1646642393834.png

1646642411955.png

Nhờ các cao nhân giúp em với :cry:
 
lỗi nó nói rõ rồi đó, expression phải là pointer, studentArrray thêm cái dấu [] này vào méo phải pointer nữa, nó là value...
Ai dạy cháu cái kiểu xóa phần tử vailon quá vậy, lên google search đại 1 cái bài nào về mảng động xem nó làm sao rồi bắt chước làm theo nhé.
 
lỗi nó nói rõ rồi đó, expression phải là pointer, studentArrray thêm cái dấu [] này vào méo phải pointer nữa, nó là value...
Ai dạy cháu cái kiểu xóa phần tử vailon quá vậy, lên google search đại 1 cái bài nào về mảng động xem nó làm sao rồi bắt chước làm theo nhé.
Em có muốn xóa kiểu này đâu thím :cry:,đề bài bắt xóa phải dùng cái destructor qua cái keyword delete ấy :cry:, pointer thì em mới học đây nên còn ngáo ngơ quá :too_sad:
 
Show đoạn decleration của studentArray xem nào. Chắc định nghĩa studentArray là array của student chứ không phải của student* thì không dùng được delete studentArray[i ] thôi :D
Sửa lại như bác trên nhé
 
Last edited:
giả sử như cái destructor của anh viết là đúng thì thử
delete studentArray + i nhé
Show đoạn decleration của studentArray xem nào. Chắc định nghĩa studentArray là array của student chứ không phải của student* thì không dùng được delete studentArray[i ] thôi :D
Sửa lại như bác trên nhé
Em thử rồi mà ko đc :cry: . Em post bài làm cho mấy thím coi thử
1646654146793.png

1646654161118.png

1646654214177.png

1646654191442.png

Đề thì ntn:
1646654270946.png

Em cũng vừa thấy 1 lỗi nữa là em thử xóa luôn toàn bộ array thì nó thoát ra khỏi vòng lặp luôn, ko cho chọn option nữa =((
#include <iostream>
#include <limits>
using namespace std;
class Student{
string name;
int score;
public:
Student(){}
Student(string name, int score){
this->name = name;
this->score = score;
}
~Student(){
cout<<"Deleted";
}
string getName(){
return name;
}
int getScore(){
return score;
}
};
int sizeOfArray = 5;
Student *studentArray = new Student[sizeOfArray];
int numberOfElements = 0;
void add(){
if (numberOfElements == sizeOfArray){
sizeOfArray *=2;
Student *newArr = new Student[sizeOfArray];
for(int i = 0; i<numberOfElements; i++){
newArr = studentArray;
}
delete[] studentArray;
studentArray = newArr;
}
string name;
int score;
cout<<"Enter name of student: ";
cin>>name;
cout<<"Enter student's score: ";
cin>>score;
while(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Wrong Input!!!"<<endl;
cout<<"Enter score again: ";
cin>>score;
}
Student *student = new Student(name,score);
studentArray[numberOfElements] = *student;
numberOfElements++;
}
void display(){
for(int i = 0; i<numberOfElements; i++){
cout<<studentArray.getName()<<" : "<<studentArray.getScore()<<endl;
}
}
void displayBestStudent(){
int large = -1000;
for(int i = 0; i <numberOfElements; i++){
if(studentArray.getScore()>large){
large = studentArray.getScore();
}
}
for (int i = 0; i < numberOfElements; i++){
if(studentArray.getScore()==large){
cout<<studentArray.getName()<<" : "<<studentArray.getScore();
}
}
}
void deleteStudent(){
string name;
cout<<"Enter a name: ";
cin>>name;
for(int i = 0; i< numberOfElements; i++){
if(studentArray.getName() == name){
delete studentArray;
}
}
}

int main()
{
string choice;
while (1) {
cout << "------------MENU------------" << endl;
cout << "1. Add new student" << endl;
cout << "2. Display all students" << endl;
cout << "3. Display best students" << endl;
cout << "4. Remove" << endl;
cout << "5. Exit program" << endl;
cout << "---------------" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == "1") {
add();
}
else if (choice == "2") {
display();
}
else if (choice == "3") {
displayBestStudent();
}else if(choice == "4"){
deleteStudent();
}
else if (choice == "5") {
break;
}
else {
cout << "Wrong input, enter your choice again!!!" << endl;
}
}
}

Mấy thím xem thử giúp em với =((
 

Attachments

  • 1646654177072.png
    1646654177072.png
    52 KB · Views: 23
Destructor để xoá mảng động. Đề này thì tạo mảng khác với số element ít hơn mảng cũ 1 phần tử rồi copy các phần tử ở mảng cũ sang. Delete cái mảng cũ đi rồi trỏ pointer sang mảng mới.
 
phải làm cái mảng
C++:
Student** studentArray = new Student*[sizeOfArray];
// gán
studentArray[i] = student; // gán con trỏ = con trỏ, ko có * ở đây

thì mới
Code:
delete studentArray[i]
được nha
1BW9Wj4.png



còn muốn xài con trỏ 1 tầng thì phải xài placement new mà ko phải yêu cầu của bài này
 
Last edited:
Object *x = new Object[N] thì là tạo mảng x với N phần tử nên phải delete x[]. Dùng std::vector đi cho sướng thân :D. Hoặc thích khổ dâm thì làm như thím trên nói
 
xoá 1 phần tử thì dùng swap các phần tử sau đó của array lên 1 giá trị, rồi giảm number xuống, đối tượng cần xoá thì trỏ qua 1 con trỏ khác rồi hã delete con trỏ đó :go: còn không thì dùng con trỏ ** hoặc dùng vector :go:
 
Object *x = new Object[N] thì là tạo mảng x với N phần tử nên phải delete x[]. Dùng std::vector đi cho sướng thân :D. Hoặc thích khổ dâm thì làm như thím trên nói
bắt buộc phải dùng mảng động thím, chứ em đâu muốn khổ râm đâu :beated:
 
Back
Top