【问题标题】:C "undefined reference", but I specified it in the same file [closed]C“未定义的引用”,但我在同一个文件中指定了它[关闭]
【发布时间】:2012-11-19 03:40:21
【问题描述】:

我正在尝试 Nintendo DS 编程,我有一个 main.cpp 文件、一个 snake.h 文件和一个 snake.cpp 文件。当我编译(使用 Makefile)时,我得到了

/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)'

这是该函数中的错误

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;
}

但在同一个文件中,大约 100 行之前,我有

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

如果我注释我使用 printBlock 的行(在 drawSnake 函数中),代码编译不会出错。 我试图改变名字,改变位置,但我不明白为什么只有那个函数给我错误。

【问题讨论】:

  • 使用 GitHub 备份代码,而不是 Dropbox...

标签: c++ reference undefined nintendo-ds


【解决方案1】:

这是一个不同的函数(五个参数而不是四个)。

你可能打算这样称呼它:

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

(其他呼叫站点也是如此。)

【讨论】:

  • 天哪,我是个白痴。谢谢!
【解决方案2】:

您缺少game 类型的第一个参数。您大概需要更改第 228 行:

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

到这里:

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

在第 230 行也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多