gpt4 book ai didi

c++ - 指向数组参数的双指针错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:51:43 31 4
gpt4 key购买 nike

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: error: invalid type argument of unary «*» (have «int»)

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

最佳答案

问题出在这一行:

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

ia[i][j] 给你一个 int 然后你尝试取消引用。看起来你真的只是想要:

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

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

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

关于c++ - 指向数组参数的双指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15310423/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com