Shabaz Terran
Shabaz Terran

Reputation: 23

Error: Cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'

I am taking an input from user for the size of an array, and then the elements of it.

In the below code, cin>>A[i] in the first for loop is giving me an error.

From other questions similar to this one, it's a simple operator error, and the script is similar to working three dimensional ones I've seen. Is new creating a 3 dimensional array by default, meaning I'd need to define columns too? If not, where would I be missing the operator?

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] << " ";
}

Upvotes: 1

Views: 3094

Answers (2)

nluk
nluk

Reputation: 56

What array do you want to create? Two-dimensional SxS, or just S-sized? Because you are creating an array of arrays, while trying to access it as a single-dimensional.

Changing int** A to int* A, A = new int*[s] to A = new int[s], and getting rid of A[i]=new int[s] in the first loop makes the code correct.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 598279

A[] is an int* pointer, not an int value.

There is no operator>> that can read an int value into an int* pointer. Since you want to read an int value, you have to read into an int variable, so change A[i] in your 1st loop to *A[i] instead:

cin >> *A[i];

You need to do the same with A[j] in the 2nd loop:

cout << *A[j] << " ";

This is because there is no operator<< to write an int value from an int* pointer, but there is an operator<< that can write the value of a memory address held by a void* pointer, and int* is implicitly convertible to void*.

Don't forget to delete[] your arrays when you are done with them:

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;

That being said, you are wasting memory for the second dimension when s > 1, since you are filling in and using only the 1st column and ignoring additional columns. The code you showed only really needs a 1-dimensional array instead:

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;

If you really want a 2-dimensional array, try something more like this instead:

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;

That being said, you really should be using std::vector instead of new[] directly.

Upvotes: 1

Related Questions