1

I was just working on a little programming project, and I got a fairly common error about something being undeclared:

MP_HighLevelData.c:230:15: error: ‘RemovedUser’ undeclared (first use in this function) 

Thinking I had just forgotten to declare the variable, I went along to the line in my source file, and found that the error was pointing at this line of code:

User *RemovedUser; 

Odd, I can't declare a new variable because it doesn't exist? I'm sure it's not specifically this line of code that's at fault, so here's a more complete code snippet. I'd really like to know what I've done wrong.

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; } } } } 
2
  • 2
    (User *User) - are you sure compiler can distinguish between the User type and User parameter? Commented Feb 23, 2013 at 7:59
  • @AlexFarber hmm. missed it. the error must be that user is not a type Commented Feb 23, 2013 at 8:01

1 Answer 1

2

Usually, when faced with the decision of 'Type' or 'Variable of type', the compile will always assume 'Variable of type', which is why accessing User as object works.

However, at the same time, declaring a new object with the type User fails to work, because for the compiler, that's a variable, not a type.

In short: Rename your variable User to anything but the type name (i.e. UserObject or something), and your code should work just fine in that regard.

For the clarification, this is my suggested fix:

void RemoveUserFromGameRoom(User *myUser) { if (myUser->GameRoom != NULL) { GameRoom *GameRoom = myUser->GameRoom; //More code to come } //Some more code } 
Sign up to request clarification or add additional context in comments.

8 Comments

The funny thing is, I have User *RemovedUser; in another function in the same source file, and it doesn't generate an error. And it comes before this function...
@WillWill56 Yeah, because THAT isn't the issue, but rather the parameter User *User. That's the variable you need to rename.
But it looks like this works, thanks. Yes, this was the only error, everything else works fine.
@WillWill56 Please accept my answer if your question has been answered with it. Thank you very much. And you're welcome :)
I tried to accept it, but I have to wait 3 minutes or something, I only just signed up :P (31 seconds to go)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.