0

I have a problem with this code snippet and i can't realise what i did wrong.

The error output is like this:

/storage/emulated/0/Documents/feladat.cpp:18:7: warning: expression result unused [-Wunused-value]
                                b[i, j] = b[a[i + 1], a[i]];
                                  ^
/storage/emulated/0/Documents/feladat.cpp:19:9: error: invalid operands to binary expression ('std::__ndk1::ostream' (aka 'basic_ostream<char>') and 'vector<vector<int> >')
                                cout<<b;
                                ~~~~^ ~
/data/data/ru.iiec.cxxdroid/files/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../include/c++/4.9.x/ostream:218:20: note: candidate function not viable: no known conversion from 'vector<vector<int> >' to 'const void *' for 1st argument; take the address of the argument with &
    basic_ostream& operator<<(const void* __p);

And it goes on with different types in the ()s

It would be a program which makes pairs into the b vector, if two number's difference is 1. Here is my code:

#include <iostream>
#include <vector>

using namespace std;
int main()
{
    vector<int> a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    vector<vector<int>> b;
    b.reserve(10);
    int i = 0;
    int j = 1;
    for (i < a.size(); i++;)
    {
        for (j <= a.size(); j++;)
        {
            if (a[i + 1] - a[i] == 1)
            {
                b[i, j] = b[a[i + 1], a[i]];
                cout << b;
            }
        }
    }
    return 0;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88

1 Answers1

2

You have 4 problems in given code snippet:

  1. This is not how you index a 2D array: b[i, j]. You index it by b[i][j]. Otherwise, you are using a comma operator to compute a single index (b[i, j] would be equivalent to b[j]).
  2. for loops are in the form of for (initialization;comparison;increment). Some parts can be empty, but semicolons must be there. Hence for (i < a.size(); i++;) should be for (;i < a.size(); i++;), or, alternatively for (int i = 0; i < a.size(); i++;). The same for j loop.
  3. Undefined behavior due to indexing (potentially unallocated) space of b. If you want to create 10 elements, of each vector, so you could use operator[], consider just allocating all of those elements via vector<vector<int>> b (10, vector<int> (10)); (or, alternatively (vector<vector<int>> b (a.size (), vector<int> (a.size ()));) instead of using reserve. reserve should be used to prevent multiple re-allocations due to multiple push_backs.

    Note, such change would make the loop for (;j <= a.size(); j++;) index invalid ranges of vector, due to it going through the values of [1, 10], while the vector would contain valid indices of [0, 9]. You may change either the loop condition, or indexing (to, for example: b[i][j-1] = b[a[i + 1]-1][a[i]-1];).

  4. std::vector doesn't have operator<<, which can be used for printing, defined. You would need to decide how would you want for the vector to be printed, and then define the function, which implements such printing.

Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17