私は小さなプログラミングプロジェクトに取り組んでいたところ、宣言されていないものについてかなり一般的なエラーが発生しました。
MP_HighLevelData.c:230:15: error: ‘RemovedUser’ undeclared (first use in this function)
変数を宣言するのを忘れたと思って、ソースファイルの行に移動したところ、エラーが次のコード行を指していることがわかりました。
User *RemovedUser;
奇妙なことに、新しい変数が存在しないために宣言できませんか?特にこのコード行に問題があるわけではないので、より完全なコードスニペットを次に示します。何を間違えたのか本当に知りたいです。
void RemoveUserFromGameRoom(User *User) {
if (User->GameRoom != NULL) {
GameRoom *GameRoom = User->GameRoom;
if (GameRoom->Owner == User) {
// We should delete the whole game room, since the owner is leaving and a new owner isn't chosen automatically
while (GameRoom->UsersHead != NULL) { // Awesome way of looping while there are users left in the room
// We need to get rid of all the users in this game room, including the owner, before we can remove it
User *RemovedUser;
RemovedUser = GameRoom->UsersHead->User;
DeleteUserPtrFromGameRoom(GameRoom->UsersHead); // Remove reference to the user from the game room
RemovedUser->GameRoom = NULL; // Remove reference to the game room from the user (needs to be set afterwards, whoops)
}
// All the users have been kicked out, now we can take care of the game room
FreeRIDfromGameCategory(GameRoom->RID, User->GameCategory);
ClearGameRoomName(GameRoom);
DeleteGameRoomFromGameCategory(GameRoom, User->GameCategory);
} else {
UserPtr *UserPtr = GameRoom->UsersHead;
while (UserPtr != NULL) {
if (UserPtr->User == User) {
DeleteUserPtrFromGameRoom(UserPtr);
User->GameRoom = NULL;
break;
}
UserPtr = UserPtr->next;
}
}
}
}