views:

352

answers:

2
void FileManager::CloseFile(File * const file)
{
 for (int i = 0; i < MAX_OPEN_FILES; ++i)
 {
  if ((_openFiles[i] == file) == true)
  {
   _openFiles[i] == NULL;
  }
 }
...

_openFiles is a private member of FileManager and is just an array of File *'s
When the exact same test is performed in the Immediate window i get a result of 1!?!

EDIT the == true was added purely as a sanity check!!

+4  A: 

You have

 _openFiles[i] == NULL;

should that be

 _openFiles[i] = NULL;

?

Charles Ma
ha! for shame !
Adam Naylor
So the comparison WASN'T failing?I assumed that "Immediate window" meant you were doing this in some kind of debugger -- did you not step through the program?Also: compile with full warnings (and even warnings as errors) in order to avoid such mistakes.
Pod
Q: What did we learn today? A1: The fewer characters the fix, the harder to find the bug. A2: Never, ever, do assignments inside conditionals.
jeffamaphone
+2  A: 

Not enough karma to add a comment, but if cma's answer was was correct (that a conditional should be an assignment), then I wish the original post hadn't been fixed, or at least had a note that it had been fixed. I stared at it a long time, trying to find the bug, before giving up and reading the answers.

To anyone who can, feel free to delete this "answer".

Mark Jones
Yea... I was wondering the same thing. I couldn't for the life of me figure out what was wrong with it because of that. :/
Zack