双指针指向数组参数时出错

时间:2013-03-09 12:00:45

标签: c++ dynamic-arrays

int main (void)
{
    int** arr = new int*[4];
    for (int i = 0; i < 4; i++) arr[i] = new int[4] {1, 0, 0, 1};

    const int* p = &(arr[0][0]);

    TFigure* test = new TFigure(arr, 4, 4);
    test->resolve();

    for (int i = 0; i < 4; i++) delete[] arr[i];
    delete[] arr;

    return 0;
}

构造函数声明的位置     第57行:

TFigure(int **ia, int n, int m)
    N = n;
    M =m;

    landscape = new int*[n];
    puddles = new int*[n];
    for (int i = 0; i < n; i++){
        landscape[i] = new int[m];
        puddles[i] = new int[n];
        for (int j = 0; j < m; j++)
            landscape[i][j] = *ia[i][j];
        }


    for (int i = 0; i < n; i++)
        for (int j = 0; j < 0; j++)
            if (i == 0 || i == N || j == 0 || j == M)
                puddles[i][j] = 0;
            else
                puddles[i][j] = 1;

    for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++)
    std::cout << puddles[i][j] << ' ';
    std::cout << std::endl;
    }
    for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++)
    std::cout << landscape[i][j] << ' ';
    std::cout << std::endl;
    }


};

但我有错误

  

57:43:错误:一元«*»的无效类型参数(有«int»

我不明白是什么原因造成的。

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

landscape[i][j] = *ia[i][j];

ia[i][j]为您提供int,然后您尝试取消引用。看起来你真的只是想要:

landscape[i][j] = ia[i][j];

我不确定复制和粘贴是否是一个错误,但是你的构造函数定义缺少一个开头{

TFigure(int **ia, int n, int m) {
//                         Here ^