具有可變大小的隊列的c++全局數組

[英]C++ Global Array of Queues with variable size


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.

我正在處理一個項目,該項目需要一個QueueArray類,它是一個隊列數組。自從我使用c++數組以來已經有一段時間了,所以我在調試代碼時遇到了一些麻煩。

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

警告C4200:使用的非標准擴展:當UDT包含一個零大小的數組時,struct/union 1>中的零大小數組不能生成copy-ctor或copy-assignment操作符

and

error C2075: 'theArray' : array initialization needs curly braces

錯誤C2075: 'theArray':數組初始化需要花括號

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.

稍后,隊列將是用戶定義的類型,但是我們現在允許它們是int類型的隊列,不確定這是否會有影響。我一直看到人們建議在這些情況下使用向量但是我沒有選擇。

Any suggestions would be appreciated.

如有任何建議,我們將不勝感激。

3 个解决方案

#1


1  

using variable sized arrays is not possible in C++. to make your code working, use a pointer, i.e.

使用可變大小的數組在c++中是不可能的。要使代碼工作,請使用指針,例如

class QueueArray
{
    queue<int> * const theArray;

    QueueArray::QueueArray(int size)
    : theArray (new queue<int>[size])
    { /* ... */ }
    ~QueueArray()
    { delete[] theArray; }
};

However, in C++ you should really avoid this and use a std::vector instead, i.e.

但是,在c++中,您應該避免這種情況,而應該使用std:::vector,即。

typedef vector<queue<int>> QueueArray;

#2


3  

queue<int>* theArray;

not

queue<int> theArray[];

#3


1  

When allocating an array on the heap you get back a pointer to the start of the array, rather than an array. Thus, you want to declare your array as

在堆上分配數組時,您將得到指向數組開始的指針,而不是數組。因此,您需要聲明您的數組。

queue<int>* theArray;

The use of empty brackets is allowed in some contexts but has specific meaning. It can be used when initializing a statically size array:

在某些上下文中允許使用空括號,但有特定的含義。在初始化靜態大小數組時可以使用:

queue<int> theArray[] = { queue<int>(), queue<int>() /*...*/ };

creates an array of a size matching the list of initializers and it still can be empty. You can also use empty brackets in the argument list of a function in which case the use is equivalent to using pointer notation, e.g.:

創建一個大小與初始化器列表匹配的數組,它仍然是空的。您還可以在函數的參數列表中使用空括號,在這種情況下,這種用法等同於使用指針表示法,例如:

int main(int ac, char* av[])

is identical to

是一樣

int main(int ac, char** av)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/09/29/36ba5f418b0c95cc6cbcfc82ea47f082.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com