错误:无法绑定&#39; std :: istream {aka std :: basic_istream <char>}&#39; lvalue to&#39; std :: basic_istream <char>&amp;&amp;&#39;

时间:2018-04-19 23:32:16

标签: c++ arrays

我正在从用户那里获取数组大小的输入,然后是它的元素。

在下面的代码中,第一个cin>>A[i]循环中的for给了我一个错误。

从与此类似的其他问题来看,这是一个简单的操作错误,而且脚本类似于我所见过的三维工作。 new默认情况下会创建一个三维数组,这意味着我还需要定义列吗?如果没有,我会在哪里错过运营商?

int** A;
int s;
cin >> s;
A = new int*[s];

for(int i=0;i<s;i++)
{
    A[i]=new int[s];
    cout<< "Enter value: ";
    cin>>A[i];
}

cout<< "Array:\n";
for(int j=0;j<s;j++)
{
    cout << A[j] << " ";
}

2 个答案:

答案 0 :(得分:1)

int*int指针,而不是operator>>值。

没有int可以将int*值读入int指针。由于您想要阅读int值,因此您必须阅读A[i]变量,因此请将第一个循环中的*A[i]更改为cin >> *A[i];

A[j]

您需要在第二个循环中对cout << *A[j] << " "; 执行相同的操作:

operator<<

这是因为没有intint*指针写入operator<<值,但是有void*可以写入内存地址的值由int*指针持有,void*可隐式转换为delete[]

完成后,不要忘记int s; cin >> s; int** A = new int*[s]; for(int i = 0; i < s; ++i) A[i] = new int[s]; for(int i = 0; i < s; ++i) { cout << "Enter value: "; cin >> *A[i]; } cout << "Array:\n"; for(int j = 0; j < s; ++j) cout << *A[j] << " "; for(int j = 0; j < s; ++j) delete[] A[j]; delete[] A; 阵列:

s > 1

话虽如此,当int s; cin >> s; int* A = new int[s]; for(int i = 0; i < s; ++i) { cout << "Enter value: "; cin >> A[i]; } cout << "Array:\n"; for(int j = 0; j < s; ++j) cout << A[j] << " "; delete[] A; 时,你正在为第二维消耗内存,因为你正在填写并仅使用第一列并忽略其他列。您展示的代码实际上只需要一维数组:

int rows, columns;
cin >> rows;
cin >> columns;

int** A = new int*[rows];
for(int i = 0; i < rows; ++i)
    A[i] = new int[columns];

for(int i = 0; i < rows; ++i)
{
    for(int j = 0; j < columns; ++j)
    {
        cout << "Enter value for (" << i << "," << j << "): ";
        cin >> A[i][j];
    }
}

cout << "Array:\n";
for(int i = 0; i < rows; ++i)
{
    for(int j = 0; j < columns; ++j)
        cout << A[i][j] << " ";
    cout << endl;
}

for(int i = 0; i < rows; ++i)
    delete A[i];
delete[] A;

如果您真的想要一个二维数组,请尝试更类似的东西:

std::vector

话虽如此,您确实应该直接使用new[]代替func drawSpiral(rotations:Double) { let scale = scaleFactor(rotations) // do some math to figure the best scale UIGraphicsBeginImageContextWithOptions(mainImageView.bounds.size, false, 0.0) let context = UIGraphicsGetCurrentContext()! context.scaleBy(x: scale, y: scale) // some animation prohibits changes! // ... drawing happens here myUIImageView.image = UIGraphicsGetImageFromCurrentImageContext() }

答案 1 :(得分:0)

您想要创建什么数组?二维SxS,还是S尺寸? 因为您正在创建一个数组数组,同时尝试以一维方式访问它。

int** A更改为int* A,将A = new int*[s]更改为A = new int[s],并在第一个循环中删除A[i]=new int[s],使代码更正。

相关问题