Pass struct matrix as argument

Asked

Viewed 228 times

1

Having the struct

struct bloco {
    bloco () : real(), marcado('.'){}

    char real;
    char marcado;
};

When I try

void tabuleiro (int lado, int mina){
    ...
    bloco tab[lado][lado];

    ...

    for (i=0, j=0; lado>i; i++){
        for (j=0; lado>j; j++){
             tab[i][j].real = '.';
        }
    }

    ...

    jogar(tab[lado][lado]);

    return;
}

And in the role play I have

void jogar(bloco tab){
    tab[x][y].marcado = 'M'; 
}

I get the message

no match for 'Operator[]' (operand types are 'block' and 'int') tab[x][y]. marked = ’M';

As far as I understand it, it means that I did not pass "tab" as being an array, so I tried to solve with:

void jogar(int lado, bloco tab[lado][lado]){
    tab[x][y].marcado = 'M'; 
}

Who gave me the following message

error: use of Parameter 'side' Outside Function body void play(int side, block tab[side][side]){

I just wanted to pass the struct matrix created in one function to change it in the other, but I don’t know how :(

1 answer

0


The second error you received is because you need to pass a constant size within the brackets in the parameter statement. " side" does not serve as it is also a parameter. Even if it was, side is not a constant or constant expression, then cannot be used as matrix dimension.

That is, you will have to fix the size of your matrix and, once fixed, do something like this:

void jogar(bloco tab[][ALTURA]) { ... }

Apparently, this doesn’t fit your requirements, so you can use std:vectors, which are of dynamic size. std::vector of std::vector, in your case.

  • 1

    Thank you very much! I used vector <vector<bloco> > tab(lado, vector<bloco>(lado)); and solved my compilation problems. Now there are other logic, but these I can solve with a little study!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.