Implementing queue

I have made a struct Queue and now i want an object for Queue but it says it doesn't not have storing capabilities , It works when i try to make it in function it accesses s front and rear
1
2
3
4
5
6
7
8
9
10
  struct Queue
{
	int items[length];
	int front ,rear;
};
Queue q;
q.front=-1;//does not work why??
void Enque(int x)
{
	q.front=1;//does work why?? 
I'm not great in programming but try creating a constructor for q.front=-1 for example
1
2
3
4
5
6
7
8
9
10
struct Queue
{
int items[length];
int front, rear;
    Queue
    {
         Queue q;
         q.front=-1;
    }
};
Yes , it does work in constructor never came into my mind to apply constructor, but still why doesn't the first thread method work ? o_0
You can't put executable code outside of a function.

1
2
3
4
5
6
7
8
9
10
11
int some_func()
{
    // do some stuff.
}

std::swap(x,y);

void other_func()
{
    // do something with x and y
}


How would you expect this code to behave? When would the swap function be called?
When would the swap function be called?

Swap function will not be called.
Sorry but i still don't understand why can't i make global instance of Queue can you ignore Enguue() function and how will be it possible to make an object of Queue and assign -1 to its front member?
You can't put executable code outside of a function.

But Queue isn't a function it's a structure.
Last edited on
Sharan123 wrote:
Swap function will not be called.

The swap function there will result in a syntax error, just as your assignment did in the OP.

Sharan123 wrote:
Sorry but i still don't understand why can't i make global instance of Queue can you ignore Enguue() function and how will be it possible to make an object of Queue and assign -1 to its front member?

You can make a global instance of Queue. You cannot assign values to that object outside of a function unless it is part of the initialization of that object.

If you insist on not defining an appropriate constructor:

1
2
3
4
5
6
7
8
9
const unsigned length = 256;

struct Queue
{
    int items[length];
    int front, rear;
};

Queue q = { {}, -1 }; // initialization  



cire wrote:
You can't put executable code outside of a function.
Sharan123 wrote:
But Queue isn't a function it's a structure.

Yes, that's what I said. You can't put executable code outside of a function.
Topic archived. No new replies allowed.