Redimensioning of array.

Iam just kind of stuck here int basic program of queue, Can anyone help??
class dq
{
int cap;
int front;
int rear;
int qu[];
public:
queue(int x)
{
cap=x;
front=-1;
rear=-1;
-------> qu[]=new int[cap];

}; }
//The above pointed line shows the Error -
Compiling NONAME00.CPP:
Error NONAME00.CPP 15: Expression syntax in function dq::queue(int)
Does it work if you remove the first pair of square brackets on that line? And because the topic is resizing/dynamic arrays, I'd like to point out http://www.cplusplus.com/reference/vector/
NOPE IT'S NOT WORKING
But it shows this error instead..
Compiling NONAME00.CPP:
Error NONAME00.CPP 15: Lvalue required in function dq::queue(int)
Warning NONAME00.CPP 130: Function should return a value in function main()

My whole code is -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<iostream.h>
#include<conio.h>
class dq
{
 int cap;
 int front;
 int rear;
 int qu[];
 public:
	queue(int x)
		{
		 cap=x;
		 front=-1;
		 rear=-1;
		 qu=new int[x];
		}
	void push (int x)
		{
		if(rear==cap-1)
		cout<<"The queue is full."<<endl;
		else if (front==rear==-1)
		{
		 front=0;
		 rear=0;
		 qu[rear]=x;
		}
		else
		{
		 rear++;
		 qu[rear]=x;
		}
		}
	void pop()
		{
		 if (front==0)
		 cout<<"There is nothing to delete in the stack."<<endl;
		 else if(front>rear)
		 {
		  front=-1;
		  rear=-1;
		 }
		 else
		 front++;
		}
	void front_push(int x)
		{
		 if(front==0)
		 {cout<<"Stack overflow"<<endl; }
		 else if(front==rear==-1)
		 {
		  front=0;
		  rear=0;
		 }
		 else
		 {
		  front--;
		  qu[front]=x;
		 }
		}
	void rear_pop()
		{
		 if(rear==cap-1)
		 {
		  cout<<"There is nothing to delete."<<endl;
		 }
		 else if (front>rear)
		 {
		  front=-1;
		  rear=-1;
		 }
		 else rear--;
		}
	void show()
		{

		 for(int i=front;i<=rear;i++)
		 {
		  cout<<qu[i];
		 }
		}
	};
 main()
 {
  char ch;
  int x,n;
  dq d1;
  a:
  cout<<"Welcome to the first queue."<<endl;
  cout<<"1.Insert using rear."<<endl;
  cout<<"2.Delete using front."<<endl;
  cout<<"3.Insert using front."<<endl;
  cout<<"4.Delete using rear."<<endl;
  cout<<"5.Show the contents."<<endl;
  cout<<"Choose your choice from 1 to 5."<<endl;
  cin>>x;
  switch (x)
  {
	case 1:
	{
	 cout<<"Enter the no you want to insert."<<endl;
	 cin>>n;
	 d1.push(n);
	}
	break;
	case 2:
	d1.pop();
	break;
	case 3:
	{
	 cout<<"Enter the no you want to insert."<<endl;
	 cin>>n;
	 d1.front_push(n);
	}
	break;
	case 4:
	d1.rear_pop();
	break;
	case 5:
	d1.show();
	default:
	{
	cout<<"Choose your choice from 1 to 5."<<endl;
	cout<<"Want to go back?(y/n)";
	cin>>ch;
	if (ch=='y')
	goto a;
	}
  }
  getch();
 }


int qu[];

You can't define an array like that, without giving the size of the array as part of the definition.

qu=new int[x];

You can't just assign a new memory address to the name of an array like that, as if it were an lvalue.

You seem to be muddling up different ways of creating arrays. Instead of declaring qu as an array, you need to declare it as a pointer, i.e. a memory address:

int* qu;

Then, when you allocate the memory for the array, the address of the start of the array gets stored in the variable.
Last edited on
Thanks!! #Mikey_boy it worked !
Topic archived. No new replies allowed.