Invalid conversion when using pointers (invalid Conversion from 'int**' to 'int' [-fpermissive])

Asked

Viewed 30 times

-3

I am learning about pointers and dynamic allocation and I cannot overcome this bug. Apparently the pointer is not accepting the value of type int, but I cannot understand why this occurs.

#include <iostream>
#include <string.h>
#include <time.h>

struct Inimigo {
    std::string nome;
    int vida;
};

struct Bloco {
    int bloqueado_ou_nao;
};

struct Mapa {
    int A;
    int L;
    Bloco matriz;
};

struct Fase {
    std::string nome;
    Mapa mapa_fase;
    int N;
    Inimigo vetor;
};

Mapa CriarMapa(int  altura, int  largura) {
    Mapa ** mapa_funcao;
    int porcentagem = 0;

    (**mapa_funcao).matriz.bloqueado_ou_nao = new int * [altura];
    for(int i = 0; i < altura; i++) {
        (*mapa_funcao[i]).matriz.bloqueado_ou_nao = new int [largura];
    }

    for(int i = 0; i < altura; i++){
        for(int j = 0; j < largura; j++) {
            porcentagem = rand() % 100 + 1;
            if(porcentagem >= 1 && porcentagem <= 20) {
                mapa_funcao[i][j].matriz.bloqueado_ou_nao = false;
            }
            else if(porcentagem >= 21 && porcentagem <= 100) {
                mapa_funcao[i][j].matriz.bloqueado_ou_nao = true;
            }
        }
    }
    
    for(int i = 0; i < altura; i++) {
        for(int j = 0; j < largura; j++) {
            std::cout << mapa_funcao[i][j].matriz.bloqueado_ou_nao;
        }
        std::cout << std::endl;
    }
}

int main() {
    srand(time(NULL));
    CriarMapa(4,4);
}

1 answer

1

struct Inimigo {
    std::string nome;
    int vida;
};

struct Bloco {
    int bloqueado_ou_nao;
};
struct Mapa {
    int A;
    int L;
    Bloco matriz;
};

struct Fase {
    std::string nome;
    Mapa mapa_fase;
    int N;
    Inimigo vetor;
};

These structures you’re using are just primitive types: a Inimigo has a life value. a simple int. A Mapa has a Bloco but a block is also a simple int.
And here comes the Fase. She has a mapa_fase that has only one Bloco that is only one int. And vector is just one int.

There are no dimensions in these things. Calling a vector or matrix variable does not create a vector or matrix (C++ has no matrices. FORTRAN has matrices. C++ has vectors only. And vector vectors and such.

If you want a "matrix" in the sense of a vector vector you must build a.

    Mapa** mapa_funcao;

This probably doesn’t do what you think it does. In C++ you can use containers, more flexible things for these things, like declaring

map<int, Bloco>  um_mapa;

And a map would be like a dictionary, with a int pointing to a Bloco. There are other containers, such as lists, vectors and sets, that can better express your model. C++ is very convenient to model these things.

back on the show

    Mapa** mapa_funcao;

that declares mapa_funcao as a pointer. That’s it. And that’s what you would use in C. But it’s just a pointer. It doesn’t create a vector or an array. It doesn’t create anything.

  • mapa_funcao is int**
  • *mapa_funcao it is then int*
  • **mapa_funcao is then a int

And that line

    (**mapa_funcao).matriz.bloqueado_ou_nao = new int* [altura];

then it doesn’t make sense, since **mapa_funcao is a int. And bloqueado_ou_nao is another int then maybe I can explain your model better and tell you how to assemble a class that describes the thing.

You tried to write a program in C. It might work in this context, but if you can use C++ you should use it. It’s much more expressive than C.

The way you tried to write CriarMapa() is almost right, However the simple is to use a class or even a struct, and in C++ the constructor of the class does this, create an instance of the structure.

Browser other questions tagged

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