【发布时间】:2022-01-16 07:08:20
【问题描述】:
我正在使用用户输入的行和列在 c++ 中开发一个二维数组,并希望为列分配内存,但我不断收到错误消息;
“int”类型的值不能分配给“int”类型的实体
我知道错误的含义,但是我该如何解决它这很烦人。 下面是我的部分代码。我也没有包括打印部分,因为我希望以后能够转置数组。
// Local variables
int rows, columns;
// Prompting the user to enter the number of rows and columns
std::cout << "please input how many rows and columns you want accordingly: " << std::endl;
std::cin >> rows >> columns;
// Creating an array on the Heap memory and sizing it by the number of rows
int* arr = new int[rows];
// Assigning the values of rows
for (int i = 0; i < rows; i++) {
// Creating a new heap for columns into arr[i]
arr[i] = new int[columns];
}
// Getting the values of rows
for (int i = 0; i < rows; i++) {
// Assigning and Getting the values of columns
for (int j = 0; j < columns; j++) {
// Enter the elements of the array
std::cout << "Please enter a number: " << std::endl;
std::cin >> arr[i][&j];
}
}
【问题讨论】:
-
std::cin >> arr[i][&j]你正在使用i的地址,你应该使用i本身。 -
为什么是
C标签?潜入更多流量?我认为它是垃圾邮件。已移除
标签: c++ debugging visual-c++ computer-science