I am working on a project that requires a class QueueArray that is an Array of Queues. It’s been a while since I worked with c++ Arrays so I’m having some trouble debugging why my code is throwing errors.
I read Delete an array of queue objects for some inspiration (along with a couple hours on Google), but I am still having errors with the following code:
#include <iostream>
#include <deque>
#include <queue>
using namespace std;
class QueueArray
{
queue<int> theArray[];
QueueArray::QueueArray(int size)
{
queue<int> theArray[] = new queue<int>[size];
//theArray[] = new queue<int>[size]; //this may be closer, but also giving errors
}
};
the errors are:
warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
and
error C2075: ‘theArray’ : array initialization needs curly braces
I’ve read up about the 2nd error, but I can seem to figure out what I need to do to fix it.
I need it to be a variable sized array, with the variable passed to the class, which is why I can not initialize the size of the array up top, and It must be of global scope so I can use it in other functions within the class (the classes can’t be passed the array through a parameter).
Later on, the queues will be of a user defined type, but we’re letting them be queues of ints right now, not sure if that makes a difference. I keep seeing people suggesting the use of vectors in these cases but I don’t have a choice on this one.
Any suggestions would be appreciated.
using variable sized arrays is not possible in C++. to make your code working, use a pointer, i.e.
However, in C++ you should really avoid this and use a
std::vectorinstead, i.e.