Madison Scodelario
Madison Scodelario

Reputation: 97

using a for each loop with pointers

First of I have 2 Classes in 2 files (both .h and .cpp files), Create.h and AI.h.

Create.h has this struct in it:

public:
    struct Cell
        {
            //some stuff
            vector<Cell*> neighbors;
            State state;
        };

Here is the enum class State (stored in the Create.h file):

enum class State : char
{
    //some states like "empty"
};

Now in AI.cpp I have a function like this:

void AI::Function(Create::Cell cell)
{
        for each (Create::Cell* var in cell.neighbors)
        {
            if (var->state == State::empty)
            {

            }
        }
}

So basically I am trying to access each individual Cell which is stored in cell.neighbors with a for each so I can do some stuff to each one of them.

According to my debugger though it doesn't even reach the if (var->state == State::empty) part. Am I using the for each wrong?

EDIT: The neighbors vector has definitely elements in it

neighbors vector

Upvotes: 1

Views: 179

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283921

If you are compiling with optimizations enabled, then an empty if statement like that might be completely removed (it has no side-effects).

(Although, I think the debugger won't let you set a breakpoint on that line, if it were removed. So this is an easy test -- try to set a breakpoint on the if itself.)

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311146

it seems that the vector is empty. You can check this printing its size before the loop. And I would like to answer some comments. This form of the for loop is MS VC++ language extension. It is not C++/CLI.

Upvotes: 0

Related Questions