Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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/13.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_Multidimensional Array - Fatal编程技术网

C++ 如何解决数组下标错误的无效类型?

C++ 如何解决数组下标错误的无效类型?,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,我正在尝试将一个3D数组传递给函数并更改其值。问题是我得到了一个错误:数组下标的类型“unsigned char[int]”无效。 主要代码: unsigned char image[height][width][BYTES_PER_PIXEL]; setBlankImage((unsigned char*)image, width, height); 功能代码: void setBlankImage(unsigned char *image, int width, int height){

我正在尝试将一个3D数组传递给函数并更改其值。问题是我得到了一个错误:数组下标的类型“unsigned char[int]”无效。 主要代码:

unsigned char image[height][width][BYTES_PER_PIXEL];
setBlankImage((unsigned char*)image, width, height);
功能代码:

void setBlankImage(unsigned char *image, int width, int height){
    for(int i = 0; i < height; ++i){
        for(int j = 0; j < width; ++j){
            image[i][j][2] = 0;
            image[i][j][1] = 0;
            image[i][j][0] = 0;
        }  
    }
}

有什么建议吗?

这里,图像是用unsigned char***类型声明的:

在这里,在调用函数之前将其强制转换为无符号字符*:

setBlankImage((unsigned char*)image, width, height);

您应该将参数更改为unsigned char***类型。

传递数组的方法至少有3种:

使用数组表示法。 使用std::vector 指向第一个位置的指针。 您的代码正在使用上面的方法3。由于它是指向单个位置的指针,因此必须使用线性代数调整指向相应单元格的指针。请参见我的评论

最简单的方法是使用std::vector

第一个选项适用于您的代码:

void setBlankImage(int image[][256][256]);

如果您可以在编译时自由更改图像的类型及其尺寸,我建议您执行以下操作:

#include <array>
#include <iostream>

template <typename T, size_t Width, size_t Height, size_t Depth>
using My3DArray = std::array<std::array<std::array<T, Depth>, Height>, Width>;

template <typename T, size_t Width, size_t Height, size_t Depth>
void setBlankImage(My3DArray<T, Width, Height, Depth>& arr, T value = 0){
    for(auto& slice0: arr) {
        for(auto& slice1: slice0) {
            for(auto& elem: slice1) {
                elem = value;
            }
        }  
    }
}

int main()
{
    My3DArray<int, 4, 3, 2> arr;
    arr[0][0][0] = 42;
    std::cout << arr[0][0][0] << std::endl;
    
    try {
        arr.at(3).at(2).at(1) = 43; // OK
        arr.at(3).at(2).at(2) = 44; // Exception
        arr[3][2][2] = 45;          // Good old SEGFAULT
    }
    catch(const std::out_of_range& e) {
        std::cout << "There's even OOB check, sweet!" << std::endl << e.what() << std::endl;
        return -1;
    }
    
    return 0;
}

每像素的高度、宽度和字节数是多少。可变长度数组在C++中不是标准的。2.正如编译器试图解释的那样,无符号字符*图像不是数组。宽度-图像宽度乘以像素100高度-图像高度乘以像素100字节/像素-是一个常量,显示一个像素中有多少字节3图像中的图像是无符号字符*,但您要取消对其的引用3次?还有一种方法是手工计算最终地址。例如,对于二维数组:int*p_location=image+row*MAX_COLUMNS+column;可以扩展到3d数组:int p_location=image+i*高度+j*宽度+2;由于函数参数没有指定数组的维数,因此必须使用上述线性代数语句访问位置。类型char***表示指针深度为3。第一个表示指向单个位置的指针。第二个表示指向单个位置的指针。第三个星号表示指向{指向单个位置的指针}的指针。换句话说,将指针传递给指针数组。OP的代码表示为内存中的连续位置,而不是指针。
#include <array>
#include <iostream>

template <typename T, size_t Width, size_t Height, size_t Depth>
using My3DArray = std::array<std::array<std::array<T, Depth>, Height>, Width>;

template <typename T, size_t Width, size_t Height, size_t Depth>
void setBlankImage(My3DArray<T, Width, Height, Depth>& arr, T value = 0){
    for(auto& slice0: arr) {
        for(auto& slice1: slice0) {
            for(auto& elem: slice1) {
                elem = value;
            }
        }  
    }
}

int main()
{
    My3DArray<int, 4, 3, 2> arr;
    arr[0][0][0] = 42;
    std::cout << arr[0][0][0] << std::endl;
    
    try {
        arr.at(3).at(2).at(1) = 43; // OK
        arr.at(3).at(2).at(2) = 44; // Exception
        arr[3][2][2] = 45;          // Good old SEGFAULT
    }
    catch(const std::out_of_range& e) {
        std::cout << "There's even OOB check, sweet!" << std::endl << e.what() << std::endl;
        return -1;
    }
    
    return 0;
}