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 -->
#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=newint[x];
}
void push (int x)
{
if(rear==cap-1)
cout<<"The queue is full."<<endl;
elseif (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;
elseif(front>rear)
{
front=-1;
rear=-1;
}
else
front++;
}
void front_push(int x)
{
if(front==0)
{cout<<"Stack overflow"<<endl; }
elseif(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;
}
elseif (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();
}
You can't define an array like that, without giving the size of the array as part of the definition.
qu=newint[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.