Adding objects to 2D Vector

I want to make a matrix using vector and instead of int I use obejects of the class Fraction
However, I am trying to add elements to it but with no luck so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  class Fraction
{
public:
    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
     os << fr.num << "/" << fr.den;

     return os;
}

void Fraction::operator=(const Fraction& fr)
{
    num = fr.num;
    den = fr.den;
}

int main()
{
    Fraction fr;

    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));

    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        for (int j = 0; j < linhas; ++j)
        {
            int x;
            vector<Fraction> temp;
            cin >> x;

            fr.num = x;
            fr.den = 1;

            temp.push_back(fr);

            mat.push_back(temp);
        }

        cout << endl;
    }


    int len = mat.size();
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
            {
                cout << mat[ i ] [ j ].num << " ";
            }

       cout << endl;
    }

}


With this I do iniatialize the matrix but it will only initialize to the rows.
It will be mat[0][0], mat[1][0], mat[1][0], mat[1][0], and if I try to access mat[0][1] it will give me garbage.

I have tried this line
mat[i][j] = temp;
but it gives me "no matching operator=" error.
Last edited on
I had to fix mat.size() //missing () on this call @ line 57
and after that it compiled and ran and did things in the forum's compiler.
I don't have the assignment operator problem.
check your compiler flags, are you running pre c++ 17 mode?
Last edited on
It compliles in my pc too, the problem is
With this I do iniatialize the matrix but it will only initialize to the rows.
It will be mat[0][0], mat[1][0], mat[1][0], mat[1][0], and if I try to access mat[0][1] it will give me garbage.


the operator problem comes when I change the line 47 to this
mat[i][j] = temp;
Last edited on
There are two problems here:

1) When you imitialise your mat vector, you're initialising it to already allocate a number of elements (the columns), and, furthermore, you're initialising each of those column vectors to have a number of elements (the lines).

However, later on, when you're putting data into the mat vector, you're not modifying the values of the existing elements - you're using push_back() to add new elements onto the end of those existing vectors, making them bigger.

Do one or the other, not both.

2) A second problem is that, for every single new Fraction object you create, you're creating a whole new vector of fractions (line 39), pushing a single object (the new one) onto that vector (45), and then pushing that vector into your top-level vector as a whole new column (47). Effectively, you're creating a whole new column for every fraction, so that each column has only 1 line.

You meed to rework the logic of your code so that you only create a column where you need to, and add multiple fractions (lines) to the same column.
Found the solution after posting the problem on stackOverflow https://stackoverflow.com/questions/59514600/adding-objects-to-2d-vector-in-c

I read the MikeyBoy answer but could not understand but now I understand how it works.

This is the working solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

class Fraction
{
public:
    Fraction() {}
    
    Fraction (int n, int d) : num(n), den(d) {}

    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
     os << fr.num << "/" << fr.den;

     return os;
}


int main()
{
    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat;

    for (int i = 0; i < linhas; ++i)
    {
        cout << endl << "Linha " << i + 1 << endl;

        vector<Fraction> temp;

        for (int j = 0; j < colunas; ++j)
        {
            int x;
          
            cin >> x;

            temp.push_back({x,1}); // or temp.push_back(Fraction(x, 1))
        }

        mat.push_back(temp);

        cout << endl;
    }

    int len = mat.size();
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
            {
                cout << mat[ i ] [ j ].num << " ";
            }

       cout << endl;
    }

}
Last edited on
Topic archived. No new replies allowed.