【发布时间】:2014-12-26 22:13:34
【问题描述】:
我正在开发一个程序,但我不断收到“对 'dosell' 的未定义引用”,我无法完全弄清楚发生了什么。这是函数的声明:
void dosell(int *cash, int *numchips);
函数的使用:
choice = menu();
// Execute the appropriate choice.
if (choice == 1) {
dobuy(&cash, &numchips);
}
else if (choice == 2) {
dosell(&cash, &numchips);
}
还有函数本身:
void dosell(int *cash, int *numchips) {
int numsell;
// Determine the number of chips to be sold.
printf("How many chips do you want to sell?\n");
scanf("%d", &numsell);
// Print out the error message if this is too much.
if (numsell > *numchips)
printf("Sorry, you do not have that many chips. No chips sold.\n");
// Execute the transaction.
else {
(*cash) += sellchips(numsell);
(*numchips) -= numsell;
}}}
【问题讨论】:
-
void dosell(int *cash, int *numchips)是否声明或定义在 ABOVE 使用else if (choice == 2) ... dosell(&cash, &numchips);调用它的位置? -
dosell()是否与调用它在同一个文件中?如果没有,您是否链接两个(所有)文件来创建程序?最后的}}}是什么?它看起来像一个语法错误,除非您不小心设法使用了 GCC 扩展——嵌套函数。你能显示确切的错误信息吗?是链接器错误还是编译器错误? -
这是一个链接器错误。链接器错过了提供
dosell()的定义/实现的对象。因此,原型设计和声明不是这里的问题,亲爱的评论者,因为后面的这些调整只影响编译器。 -
相关(如果不是重复的话):stackoverflow.com/q/5892056/694576
-
@alk:相关,是的 - 它涵盖了一般情况。重复:我认为不会。目前,我寄希望于
}}}成为解决方案——这意味着dosell()函数是在内部定义的,因此只能从其他一些不是调用dosell()的函数的函数访问。但我们确实需要 OP 确认他正在使用 GCC(或者可能与 GCC 兼容的clang/LLVM),以及此时问题或代码中是否存在拼写错误。
标签: c function undefined-reference