Difficulties in making the G2 library work in C code

Asked

Viewed 50 times

0

I have this code in which I get the following errors in the terminal, and the goal is to be shown with the G2 library:

codigocanvas.c: In function ‘world_canvas_new’:

codigocanvas.c:22:33: error: expected identifier before ‘(’ token

     *device = g2_open_X11(wrld->(sw->xdim) * 10, wrld->(sw->ydim) * 1

                                 ^
codigocanvas.c:22:15: error: too few arguments to function ‘g2_open_X11’

     *device = g2_open_X11(wrld->(sw->xdim) * 10, wrld->(sw->ydim) * 1

               ^
In file included from codigocanvas.c:5:0:

/usr/include/g2_X11.h:28:5: note: declared here

 int g2_open_X11(int width, int height);

     ^
codigocanvas.c:16:36: warning: parameter ‘wrld’ set but not used [-Wunused-

but-set-parameter]

 CANVAS world_canvas_new(SHOWWORLD *wrld) {

                                    ^
codigocanvas.c: In function ‘world_canvas_update’:

codigocanvas.c:35:38: error: dereferencing pointer to incomplete type

‘SHOWWORLD {aka struct showworld}’

     for (unsigned int y = 0; y < wrld->ydim; ++y) {

                                      ^
codigocanvas.c:38:34: warning: implicit declaration of function ‘world_get’

 [-Wimplicit-function-declaration]

             AGENT *a = (AGENT *) world_get(wrld, x, y);

                                  ^
codigocanvas.c:38:24: warning: cast to pointer from integer of different size

 [-Wint-to-pointer-cast]

             AGENT *a = (AGENT *) world_get(wrld, x, y);

                        ^
<builtin>: recipe for target 'codigocanvas.o' failed

make: *** [codigocanvas.o] Error 1

    #include "codigocanvas.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <g2.h>
    #include <g2_X11.h> 
    #include "example.c"

    /* Cores que se vão utilizar */
    enum g2_colors {
        WHITE = 0,
        BLACK = 1,
        GREEN = 7,
        YELLOW = 25
    };

    CANVAS world_canvas_new(SHOWWORLD *wrld) {

        int *device;
        device = malloc(sizeof(int));

        /* Abre janela com g2 */
        *device = g2_open_X11(wrld->xdim * 10, wrld->ydim * 10);

        /* Especificar tamanho e forma dos agentes */
        g2_set_QP(*device, 25, QPrect);

        return (CANVAS) device;
    }
        /* Desenhar os zombies e os humanos  */
    void world_canvas_update(CANVAS cnvs, SHOWWORLD *wrld) {

        int device = *((int *) cnvs);
        g2_clear(device);

        for (unsigned int y = 0; y < wrld->ydim; ++y) {
            for (unsigned int x = 0; x < wrld->xdim; ++x) {

                AGENT *a = (AGENT *) world_get(wrld, x, y);

                if (a != NULL) {
                    switch (a->type) {
                        case Zombie:
                            g2_pen(device, GREEN);
                            g2_plot_QP(device, x, y);
                            break;
                        case Human:
                            g2_pen(device, YELLOW);
                            g2_plot_QP(device, x, y);
                            break;

                        default:
                            /* Se der erro esta mensagem aparece */
                            fprintf(stderr, "Agente desconhecido!");
                    }
                }
            }
        }

    }

    void world_canvas_destroy(CANVAS cnvs) {

        g2_close(*((int *) cnvs));
        free(cnvs);

    }
  • l code publicado le falta el contenido del Archivo de Ncabezado: 'codigocanvas. h'

  • Cuando llame a cualquiera de las funciones de Asignación de pila, siempre marque (! = NULL) el valor devuelto para asegurarse de que la operación fue exitosa.

1 answer

0

The syntax wrld->(sw->xdim) is invalid. The compiler is expecting a function name before the parentheses because he thinks that sw->xdim is the argument of a function call that comes after the arrow.

Exchange for (*wrld).(sw->xdim) and at least this line will compile. There may be other errors after that, of course.

  • Pablo, after making the change the error remains.

  • The same exact mistake?

  • yes "codigocanvas. c: In Function world_canvas_new': codigocanvas. c:22:28: error: Dereferencing Pointer to incomplete type ːSHOWWORLD {aka struct showworld}' *device = g2_open_X11((*wrld). (sw->xdim) * 10, (*wrld). (sw->ydim) codigocanvas. c:22:35: error: expected Identifier before ː(' token *device = g2_open_X11((*wrld). (sw->xdim) * 10, (*wrld). (sw->ydim) codigocanvas. c:22:15: error: Too few Arguments to Function ːg2_open_X11' *device = g2_open_X11((*wrld). (sw->xdim) * 10, (*wrld). (sw->ydim)"

Browser other questions tagged

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