Hà Link
Hà Link

Reputation: 377

Expected Unqualified-Id before 'delete' function

I wrote a C++ program, this error appeared and I can not find the cause. Can anybody help me. This function is used to delete element i-th from a linked list, even tried my best but I can't find the reason.

#include <cstdio>
#include <fstream>

using namespace std;

struct node
{
    int value;
    node * next;
};

typedef struct node list;

list* head = NULL;
int list_length = 0;

bool empty(){
    return (head == NULL);
}

void delete(int i){
    if(i>list_length) return;
    if(empty()) return;

    int count = 0;
    list* curr = head;
    while(curr != NULL && count < i-1){
        curr = curr -> next;
        count++;
    }
    list* temp = curr -> next;
    curr next = temp -> next;
    list_length--;
}

int main(){
}

Upvotes: 4

Views: 17157

Answers (4)

Pranesh Pyara Shrestha
Pranesh Pyara Shrestha

Reputation: 151

delete is a reserved keyword in C++. After renaming your function, it works.

Upvotes: 0

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158559

There are two errors in this code, you have named your function delete but delete is a keyword in C++, the second problem is this line in the delete function:

curr next = temp -> next;

which looks like it should be:

curr->next = temp -> next;

Upvotes: 1

awesoon
awesoon

Reputation: 33691

delete is a reserved keyword in C++. You have to rename your function.

Upvotes: 4

Andrew White
Andrew White

Reputation: 53516

You have a method called delete but delete is a keyword in C++.

Upvotes: 11

Related Questions