C "undefined reference", but I specified it in the same file

127 Views Asked by At

I am trying Nintendo DS programming, and I have a main.cpp file, a snake.h file and a snake.cpp file. When I compile (using the Makefile) I got

/home/david/Dropbox/sources/ds/04_snake/source/snake.cpp:228: undefined reference to `printBlock(int, int, unsigned short, int)'
/home/david/Dropbox/sources/ds/04_snake/source/snake.cpp:230: undefined reference to `printBlock(int, int, unsigned short, int)'

that are errors in this function

void drawSnake(game g) {
    int i;
    printBlock(g->s->last[0], g->s->last[1], g->bgColor, g->block);
    for(i=0; g->s->points[i][0]!=-1 && i<MAXLENGHT; i++)
        printBlock(g->s->points[i][0], g->s->points[i][1], g->s->color, g->block);
    return;
}

But in the same file, about 100 lines before, I have

int printBlock(game g, int x, int y, u16 color, int thickness) { /*code*/ }

And if I comment the lines where I use printBlock (in the drawSnake function) the code compiles without errors. I tried to change name, to change positions, but I can't figure why only that function gives me error.

2

There are 2 best solutions below

0
On

That's a different function (five arguments instead of four).

You probably meant to call it like this:

printBlock(g, g->s->points[i][0], ...
           ^ THIS

(The same goes for the other call site.)

0
On

You're missing the first argument, of type game. You presumably need to change line 228:

    printBlock(g->s->last[0], g->s->last[1], g->bgColor, g->block);

to this:

    printBlock(g, g->s->last[0], g->s->last[1], g->bgColor, g->block);

and similarly on line 230.