1

我遇到了删除功能的问题。我尝试以两种方式声明 head 变量(如下所示,一个已注释),但我仍然收到该错误。问题是当我返回可变头时。我指出了导致问题的行。我很感激任何帮助!

Here is the piece of the code:

class books{
public:
    char *author;
    char *title;
    int borrowed;

    books(char *a, char *t, int i){
        author=new char[20];
        title=new char[20];
        strcpy(author, a);
        strcpy(title,t);
        borrowed=i;
    };
    void display();
};

class literature: public books{
    char *type_book;
    //literature *head; //first attempt
    literature *next;
public:
    literature(char *a, char *t, int i, char *type):books(a,t,i){
        head=NULL;
        type_book=new char[20];
        strcpy(type_book,type);
    };
    void display(); // displays the list
    literature delete(char *); // delete a book by title
};
literature *head; // second attempt

literature literature :: delete(char *title){
    literature *p, *q;
    if(head!=NULL){
        if(strcmp(head->title, title)==0){
            q=head;
            head=head->next;
            delete q;
            return head; --> problem here
        }
        q=head;
        while(q->next!=NULL && strcmp(q->next->title,title)!=0)
            q=q->next;
        if(q->next!=NULL && strcmp(q->next->title,title)==0){
            p=q->next;
            q->next=q->next->next;
            delete p;
        }
        else{ 
            cout<<"\nThis author is not in the database."; 
        };
        return head; --> problem here
    }
    else{
        printf("\nEmpty list!");
        return head; --> again, same problem
    }
}
4

0 回答 0