所以我有这个非常简单的程序,但我似乎无法摆脱一个简单的错误。未定义的引用“功能”
我有这个
#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()从我的主,我包含头文件它给了我一个未定义的参考。其他awnsered问题没有帮助。
#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.o'链接,或添加'FunctionLookup.c'编译器调用。 – 2013-05-13 11:26:38
不要单独定义'false','true'。现代C应该在头文件'stdbool.h'中提供它们。如果(但是请不要除非你有真正的理由)以相反的方式定义它们,以便自然地用C表示,即'0'是错误的,并且非零值是真的。这里有'true == 0'和'false == 1'。 – 2013-05-13 11:29:56
Daniel Fischer:我使用codeblocks作为编辑器,之前从未遇到过这个问题,而我制作了一打这些程序。它是如何突然不得不改变的东西 Jens Gustedt:我知道已经删除它;) – flexzican 2013-05-13 11:32:21