Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中构造控件中的二维数组_C++_Arrays_Constructor - Fatal编程技术网

C++中构造控件中的二维数组

C++中构造控件中的二维数组,c++,arrays,constructor,C++,Arrays,Constructor,我正在努力做到以下几点: class Test{ private: int x; int y; // Create array[][] here public: Test(const int x, const int y){ this->x = x; this->y = y; // set: array[x][y] here } }; 如您所见,我想创建一个2d数组,而边界将在构造函数中给出。

我正在努力做到以下几点:

class Test{
private:
    int x;
    int y;
    // Create array[][] here
public:
    Test(const int x, const int y){
        this->x = x;
        this->y = y;
        // set: array[x][y] here
    }
};
如您所见,我想创建一个2d数组,而边界将在构造函数中给出。 我怎样才能做到这一点

它与一个常用数组一起工作:

class Test{
private:
    int x;
    int y;
    int *array;
public:
    Test(const int x, const int y){
        this->array = new int[x]; // works
        // this->array = new int[x][y] does not work
        this->x = x;
        this->y = y;
    }
};

你应该考虑用指针分配内存,应该如下:

class Test{
private:
    int x;
    int y;
    int **array;
public:
    Test(const int x, const int y){
        int i;
        this->array = new int*[x]; // first level pointer asignation
        for(i=0;i<x;i++){
            this->array[x] = new int[y]; // second level pointer asignation
        }
        // this->array = new int[x][y] does not work
        this->x = x;
        this->y = y;
    }
};

请参阅。

您可能会遇到这样的问题:除了讨论的最左边的维度外,所有维度都必须是常量

相反,请尝试以下操作:

class Test {
private:
        int x;
        int y;
        int** myArray;
public:

        Test(const int x, const int y) : x(x), y(y) {
                myArray = new int*[x];

                for (int firstDimension = 0; firstDimension < x; firstDimension++) {

                        myArray[firstDimension] = new int[y];

                        for (int secondDimension = 0; secondDimension < y; secondDimension++) {
                                myArray[firstDimension][secondDimension] = secondDimension;
                        }
                }
        }

        ~Test() {
                for(int i = 0; i < x; i++)
                        delete[] myArray[i];
                delete[] myArray;
        }
};

请参阅本文:使用std::vector而不是原始指针您可以使用模板吗?如果忘记添加析构函数释放数组,则示例将泄漏。可能值得考虑使用单个std::vector而不是std:vector。另外,根据需要,最好在初始化列表中实际创建数组,而不是在构造函数主体中创建数组:Testconst unsigned x,const unsigned y:x_x,y_y,buffer_x*y{}将立即提供具有默认值的有效数组,而不是未初始化的值。访问元素的算法应该足够简单。的可能重复