c - 对 "function"的 undefined reference

标签 c reference undefined

所以我有这个非常简单的程序,但我似乎无法摆脱一个简单的错误。

我有一个包含此的头文件

    #ifndef FUNCTIONLOOKUP_H_INCLUDED
    #define FUNCTIONLOOKUP_H_INCLUDED


    enum functions
    {
      foo,
      bar
    };

   //predefined function list
   int lookUpFunction(enum functions);

   #endif // FUNCTIONLOOKUP_H_INCLUDED

在 src 文件中我有 LookUpFunction 的定义

现在,当我从包含头文件的主函数调用lookUpFunction()时,它给了我一个 undefined reference 。其他提出的问题没有任何帮助。

   #include <stdio.h>
   #include <stdlib.h>
   #include "FunctionLookUp.h"

   int main()
   {
     lookUpFunction(foo);  <---
     return 0;
   }

功能实现

    #include <stdio.h>
    #include "FunctionLookUp.h"

    typedef void (*FunctionCallback)(int);
    FunctionCallback functionList[] = {&foo, &bar};

   void foo(int i)
   {
     printf("foo: %d", i);
   }

   void bar(int i)
   {
     printf("bar: %d", i);
   }

   int lookUpFunction(enum functions)
   {
     int test = 2;

    //check if function ID is valid
    if( functions >= sizeof(functionList))
    {
       printf("Invalid function id"); // error handling
       return 0;
    }

    //call function
    functionList[functions](test);
    return 1;
    }  

我似乎无法弄清楚这个错误来自哪里。

最佳答案

您必须有一些类似于以下内容的文件:

/* FunctionLookUp.c */
#include "FunctionLookUp.h"

int lookUpFunction(enum functions)
{
  /* code ... */
  return x;
}

某个地方可以解决您的问题

关于c - 对 "function"的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16520908/

相关文章:

c++ - 成员变量作为引用

javascript - 对象属性在 localStorage 之后未定义

javascript - 未定义的 var module.exports

C 为什么不相关/未声明的变量会影响另一个变量的输出?

c - 选择1号从给定的集合中随机

c++ - 当类成员是引用时,无法生成默认赋值运算符?(在 C++ 中)

java - 从 java.lang.reflect.Method 对象获取方法引用

c++ - MinGW 找不到 XInput.h header

c++ - 让任务运行固定数量的指令?

joomla - PhpStorm - Joomla 项目中的未定义类