-7

Can someone explain why my code is not correct? It gives an error of:

error C2065: 'i' : undeclared identifier

void CDeathMatch::RemoveViewer( CUser* pUser )
{
    if( IsValidObj( pUser ) )
    {
        if( FindPlayer( pUser->m_idPlayer ) == 0 )
        {
            BOOL bFound = FALSE;
            for( size_t  i = 0; i < m_vecDeathMatchViewer.size(); i++ )
            {
                if( m_vecDeathMatchViewer[i] == pUser->m_idPlayer )
                {
                    bFound = TRUE;
                    break;
                }
            }
            if( bFound )
                m_vecDeathMatchViewer.erase( m_vecDeathMatchViewer.begin() + i );
        }
    }
} 
Ron
  • 14,674
  • 4
  • 34
  • 47
  • 5
    You're trying to use `i` outside the for loop. – Henri Menke Oct 11 '17 at 11:17
  • 2
    in this case `i` is declared in the scope of the `for` loop and, thus, is only accessible from within that `for` loop. And you are trying to access `i` later in the `if(bFound)` – igagis Oct 11 '17 at 11:17
  • You cannot use in m_vecDeathMatchViewer.erase( m_vecDeathMatchViewer.begin() + i ); it only exists in the for loop. – Rob Oct 11 '17 at 11:17
  • 1
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Oct 11 '17 at 11:18
  • so i will erase the if( bFound ) statement? – Popem Dada Oct 11 '17 at 11:21
  • `BOOL`What's that? C++ has a type bool! –  Oct 11 '17 at 11:23
  • @manni66 `A Boolean variable (should be TRUE or FALSE). This type is declared in WinDef.h as follows: typedef int BOOL;` [https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx] – UKMonkey Oct 11 '17 at 11:29
  • @UKMonkey that's an int variable. To cite myself: _C++ has a type bool!_ –  Oct 11 '17 at 11:39
  • @manni66 you asked what it was – UKMonkey Oct 11 '17 at 11:39
  • @UKMonkey not really. –  Oct 11 '17 at 11:40

2 Answers2

2

You're trying to acess i outside it's scope. The variable is declared in the loop scope, your last if statement is outside this scope. You can declare it before the loop or rewrite your code so the last if statement is in the loop.

Murzinio
  • 74
  • 1
  • 3
0

As @Murzinio showed, the i variable is in the scope of the for loop, and the if statement is not in the same scope.

Below, I've shown a somewhat improved version with comments on why the changes were made:

void CDeathMatch::RemoveViewer( CUser* pUser )
{
    if( IsValidObj( pUser ) )
    {
        if( FindPlayer( pUser->m_idPlayer ) == 0 )
        {
            // First, you could use bool instead of BOOL as @mani66 suggested,
            // however in this context the variable is unnecessary as
            // the if statement can be eliminated
            // Also, changed the postfix increment (i++) to a prefix
            // increment (++i), as this eliminates the need for a
            // temporary copy of i (this is what the postfix increment operator does)
            for( size_t  i = 0; i < m_vecDeathMatchViewer.size(); ++i )
            {
                if( m_vecDeathMatchViewer[i] == pUser->m_idPlayer )
                {
                    // The contents of the if statement were placed here
                    m_vecDeathMatchViewer.erase(m_vecDeathMatchViewer.begin() + i );
                    break;
                }
            }
            // If statement here was eliminated
        }
    }
}

This code will have the same effect as a correct version of your code would have, with the added benefit of removing an unnecessary temporary variable.

I would indeed recommend reading up some more on c++. If you do not have access to any literature, I always found http://www.cplusplus.com/doc/tutorial/ to be very helpful.

Qub1
  • 1,154
  • 2
  • 14
  • 31