goelakash
goelakash

Reputation: 2519

Error in inserting pair element in STL queue

I have a queue of pair elements, which I am using to perform BFS on a rectangular grid. But I get an error in push operation on the queue, although I perform it similarly elsewhere.

Code:

int get_ind(int &ii,int &jj,int i,int j,int x)
{
    queue <pair<int,int> > q;
    q.push({i,j});
    while(!q.empty())
    {
        int ti = q.front().first;
        int tj = q.front().second;
        q.pop();

        for(int p=-1; p<=1; p++)
        {
            for(int q=-1; q<=1; q++)
            {
                ii = ti+p;
                jj = tj+q;
                if(issafe(ii,jj))
                {
                    q.push({ti+p,tj+q});
                    if (arr[ii][jj]==x)
                        return 0;
                }
            }
        }
    }
}

Error:

error: request for member 'push' in 'q', which is of non-class type 'int'
                     q.push({ti+p,tj+q});

Upvotes: 1

Views: 321

Answers (2)

CodeLearner
CodeLearner

Reputation: 33

The variable q has been re-declared as int and the same is being used for push operation. Use different variable either in the below for loop or in the declaration of queue.

for(int q=-1; q<=1; q++)
{
    ii = ti+p;
    jj = tj+q;
    if(issafe(ii,jj))
    {
        q.push({ti+p,tj+q});
        if (arr[ii][jj]==x)
            return 0;
    }
}

The data type of q will be int withing the scope of for loop.

Upvotes: 1

Jarod42
Jarod42

Reputation: 217293

You hide q in your loop:

queue <pair<int,int> > q;
// later
for(int q=-1; q<=1; q++)

Use different variable name in your loop.

Upvotes: 1

Related Questions