简体   繁体   中英

was not declared in this scope

I'm getting back to c++ because they want that from me for the school but I ran into problem.

I defined struct like this:

struct Member{
    string Name;
    string Surname;
    Member* Next;
};

and 2nd structure:

struct List{
    string Name;
    Member* First;
    int Size;
};

than below I had a function:

int AddToList(Member* Member, List* List){
    if(List->First == nullptr){
        List->First = Member;
        List->First->Next = nullptr;
        return 1;
    }
    Member* Current = List->First->Next;
    while(Current != nullptr){
        Current = Current->Next;
    }
    Current = Member;
    Current->Next = nullptr;
    return 2;
}

But I get an error than the line:

Member* Current = List->First->Next;

error: 'Current' was not declared in this scope.

I tried to change the function like this:

int AddToList(Member* Member, List* List){
    Member* Current = new Current;
    if(List->First == nullptr){
        List->First = Member;
        List->First->Next = nullptr;
        return 1;
    }
    Current = List->First->Next;
    while(Current != nullptr){
        Current = Current->Next;
    }
    Current = Member;
    Current->Next = nullptr;
    return 2;
}

But I get the same error just on the line:

Member* Current = new Current;

Although I though this won't helped but I just wanted to try. I though the function wasn't aware of what Member* means but than it would also have problems with the arrows and stuff as it wouldn't know whats it made of. It seems weird to me than only that 1 line seems to not know what Member* is.

You have variables with the same names as their types:

int AddToList(Member* Member, List* List) {
    // Member and List are the parameters, *not* the types
    ...
    Member* Current = List->First->Next; // syntax error, Member is a variable
}

You need to change the name of these variables to something else. A common convention in C++ is to use either lowerCamelCase or snake_case for variable names:

int AddToList(Member* member, List* list) {
    // Member and List are the types
    // member and list are the parameters
    ...
    Member* current = list->first->next; // Works now
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM