简体   繁体   English

将 function 传递给 C 中的另一个 function。 我究竟做错了什么?

[英]Passing a function to another function in C. What am I doing wrong?

Got some C code I'm working on, and it looks like it should work.我正在处理一些 C 代码,它看起来应该可以工作。 When I try to link the object files, I get an error saying "undefined reference to outputBus" and so on for each of them in the getLine function in main.当我尝试链接 object 文件时,我在 main 的 getLine function 中收到一条错误消息,指出“未定义对 outputBus 的引用”等等。 I've tried it with and without the ampersand before the function names.我已经尝试过在 function 名称之前使用和不使用&符号。 compiling with gcc -ansi .gcc -ansi编译。 What am i doing wrong?我究竟做错了什么?

typedef void(*DualOutput)(const int, const int);
typedef void(*TripleOutput)(const int, const int, const double);

void   passenger(node**, node**, itemtype*);
double bus(node**, node**, itemtype*);
int    getLine(itemtype*, DualOutput, DualOutput);
void   getLines(node**, node**, DualOutput, DualOutput, TripleOutput);
void   outputBus(const int, const int);
void   outputPeople(const int, const int);
void   outputTotal(const int, const int, const double);

int main(int argc, char **argv){
  node *head = NULL;
  node *tail = NULL;

  getLines(&head, &tail, outputBus, outputPeople, outputTotal);
  return 0;
} 

Do those functions have bodies anywhere in your code?这些函数在你的代码中的任何地方都有主体吗? All of those lines before the main function are only function declarations (aka prototypes).主要 function 之前的所有这些行都只是 function声明(又名原型)。 You actually have to create and implement the function somewhere.您实际上必须在某处创建和实现 function。

A function declaration is a promise to the compiler that the function will exist somewhere later in the code (or in another object file), so it doesn't complain. A function declaration is a promise to the compiler that the function will exist somewhere later in the code (or in another object file), so it doesn't complain. Then the linker sees that you broke your promise and gets upset.然后 linker 看到您损坏了 promise 并感到不安。

"Undefined reference" is a linker error that means the linker cannot find the implementation of the method. “未定义引用”是一个 linker 错误,这意味着 linker 找不到该方法的实现。 If it's defined in a separate source file, are you compiling and linking both of them together?如果它是在单独的源文件中定义的,您是否将它们编译并链接在一起? If it's in a library, are you linking to the library (with gcc's -lfoo option for libfoo)?如果它在库中,您是否链接到库(使用 gcc 的 libfoo 的 -lfoo 选项)?

Some "is it plugged in?"一些“它插入了吗?” type questions to get you started:输入问题以帮助您入门:

  • Did you actually implement the functions outputBus() , outputPeople() , and outputTotal() anywhere?您是否真的在任何地方实现了函数outputBus()outputPeople()outputTotal()

  • If so, was that module included in the link?如果是这样,该模块是否包含在链接中?

As written, the output functions are declared but not defined.如所写,output 函数已声明但未定义。 Without a definition in some object file included in the link they are indeed undefined references.如果链接中包含的某些 object 文件中没有定义,它们确实是未定义的引用。 The linker is trying to tell you that you need to define those functions. linker 试图告诉您您需要定义这些功能。

You've declared functions but don't implement them anywhere.您已经声明了函数,但没有在任何地方实现它们。 This just declares a function:这只是声明了一个 function:

void   outputBus(const int, const int);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM