Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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_G++ - Fatal编程技术网

C++ 错误“数组下标的无效类型”是什么意思?

C++ 错误“数组下标的无效类型”是什么意思?,c++,arrays,g++,C++,Arrays,G++,我可以看到一些关于这个错误的引用,尽管答案似乎都解决了最初的编译错误,但没有一个解释错误的真正含义 我正在使用:g++-Wall-std=c++11 myfile.cpp编译我的cpp文件,并得到以下错误: myfile.cpp: In function ‘void GenerateMatrix(uint8_t**, uint8_t)’: myfile.cpp:32:39: error: invalid types ‘uint8_t {aka unsigned char}[uint8_t {ak

我可以看到一些关于这个错误的引用,尽管答案似乎都解决了最初的编译错误,但没有一个解释错误的真正含义

我正在使用:g++-Wall-std=c++11 myfile.cpp编译我的cpp文件,并得到以下错误:

myfile.cpp: In function ‘void GenerateMatrix(uint8_t**, uint8_t)’:
myfile.cpp:32:39: error: invalid types ‘uint8_t {aka unsigned char}[uint8_t {aka unsigned char}]’ for array subscript
    std::cout << ", " << (*matrix)[i][j];
我的代码:

#include <iostream>

//// populates an n x n matrix.
//// @return the matrix 
void GenerateMatrix(uint8_t** matrix, uint8_t n)
{

    *matrix = (uint8_t*)malloc(n * n);

    uint8_t* pc = *matrix;
    for(uint8_t i = 0; i < n; i++)
    {
        for(uint8_t j = 0; j < n; j++)
        {
            *pc++ = i+j;
        }
    }

    for(uint8_t i = 0; i < n; i++)
    {
        for(uint8_t j = 0; j < n; j++)
        {
            std::cout << ", " << (*matrix)[i][j];
        }
        std::cout << "\n";
    }
}


int main()
{
    uint8_t* matrix = nullptr;
    uint8_t n = 10;
    GenerateMatrix(&matrix, n);
    return 0;
}
我曾尝试将第二个for循环中的I和j更改为int。这给了我一个类似的错误,但这次投诉是关于无效类型“uint8_t{aka unsigned char}[int]”,我仍然不知道

有人能帮我理解这个错误吗

void generateMatrix(uint8_t** matrix, uint8_t n)
//                         ^^
{
    (*matrix) // type is uint8_t*
    [i]       // type is uint8_t
    [j];      // ???
}
你实际做的相当于:

uint8_t n = 10;
n[12] = 7;   // no index ('subscript'!) applicable to raw unsigned char
             // or with compiler words, the unsigned char is an invalid
             // type for this operation to be applied on...
同样的消息也可能出现在另一个方向:

class C { }; // note that there's no cast operator to some integral type provided!

int array[7];
C c;
array[c]; // just that this time it's the other operand that has invalid type...

这是C++,你不应该使用MalC/C,而是新的UIT88T [N*N]和Dele[]矩阵,你没有免费,所以你实际上产生了内存泄漏…例外:您正在编写的代码也可以从C显式使用,因此,如果数组是由某个外部C函数直接或间接返回的。您声明了一个线性数组,其线性元素的数量为N*N,您没有声明一个二维数组。@acocagua。是的,100%同意。然而,即使我做了新的uint8_t[n][n]。我仍然得到同样的编译错误。@BithikaMookherjee啊,应该把我的评论标记为“离题”…@Aconcagua哈哈,好的,要点是: