函数指针编译错误

时间:2013-12-25 01:20:30

标签: c pointers function-pointers x86-64

当我练习自我学习函数指针时,出现了这样的错误

Undefined symbols for architecture x86_64:
  "_comp_employee", referenced from:
      _main in main-JEO5Je.o
  "_init_database", referenced from:
      _main in main-JEO5Je.o
  "_print_database", referenced from:
      _main in main-JEO5Je.o
  "_sort", referenced from:
      _main in main-JEO5Je.o
  "_swap_employee", referenced from:
      _main in main-JEO5Je.o
ld: symbol(s) not found for architecture x86_64

我无法理解为什么会发生这种情况。我认为所有这些来源(下面)都是正确的。

这是我的代码。 employee.h定义员工的信息并定义数据库和功能。

  1 /* file employee.h */
  2 
  3 #ifndef employee_h
  4 #define employee_h
  5 
  6 typedef struct emp_struct {
  7     char name[100];
  8     int employee_no;
  9     float salary, tax_to_date;
 10 } Employee;
 11 
 12 typedef Employee *Database[10];
 13 
 14 int comp_employee (int *database[], int i, int j);
 15 void swap_employee (int *data[], int i, int j);
 16 
 17 /* read in database (for this exercise fake it) */
 18 void init_database( Database employees, int no_employees );
 19 
 20 /* print out the database */
 21 void print_database ( Database people, int no_employees);
 22 
 23 #endif /* employee_h */ 

Sort.h文件是定义函数指针

  1 /* file sort.h */
  2 
  3 #ifndef sort_h
  4 #define sort_h
  5 typedef int (*comp_ptr) (int *data[], int s, int t);
  6 typedef void(*swap_ptr) (int *data[], int s, int t);
  7 
  8 void sort (int *data[], int n, comp_ptr compare, swap_ptr swap);
  9 
 10 #endif /* sort.h */

最后,main.c将其所有标题组合到程序中。

  1 #include "sort.h"
  2 #include "employee.h"
  3 
  4 int main(int argc, char *argv[]) {
  5     const int no_employees = 10;
  6     Database people;
  7     init_database (people, no_employees);
  8     print_database(people, no_employees);
  9     sort((int**)people, no_employees, comp_employee, swap_employee);
 10     print_database(people, no_employees);
 11     return 0;
 12 }

首先,我认为我的编译器在<sort.h>"sort.h"之间混淆,这是我定义的。但是在更改了标题名称后,它也会抛出相同的错误。你能给我一些关于功能指针的建议和提示吗?

2 个答案:

答案 0 :(得分:1)

您没有链接器抱怨的功能的功能定义。将以下行添加到`main的底部,它将修复链接问题:

int main(int argc, char *argv[])
{
    ...
}

int comp_employee (int *database[], int i, int j)
{
}

void swap_employee (int *data[], int i, int j)
{
}

void init_database( Database employees, int no_employees )
{
}

void print_database ( Database people, int no_employees)
{
}

void sort (int *data[], int n, comp_ptr compare, swap_ptr swap)
{
}

这些只是存根。您必须实现它们才能使逻辑起作用。

答案 1 :(得分:1)

你有几件事让我头疼,但这就是我如何编译 -

$ cat sort.h
/* file sort.h */

#include "employee.h"

#ifndef sort_h
#define sort_h
typedef int (*comp_ptr) (Database data, int s, int t);
typedef void(*swap_ptr) (Database data, int s, int t);

void sort (Database data, int n, comp_ptr compare, swap_ptr swap);

#endif /* sort.h */
$ cat employee.h 
/* file employee.h */

#ifndef employee_h
#define employee_h

typedef struct emp_struct {
    char name[100];
    int employee_no;
    float salary, tax_to_date;
} Employee;

typedef Employee *Database[10];

int comp_employee (Database database, int i, int j);
void swap_employee (Database data, int i, int j);

/* read in database (for this exercise fake it) */
void init_database( Database employees, int no_employees );

/* print out the database */
void print_database ( Database people, int no_employees);

#endif /* employee_h */ 
$ cat main.cc
#include "sort.h"
#include "employee.h"

int comp_employee (Database database, int i, int j) {}
void swap_employee (Database data, int i, int j) {}
void init_database( Database employees, int no_employees ) {}
void print_database ( Database people, int no_employees) {}
void sort (Database data, int n, comp_ptr compare, swap_ptr swap) {}

int main(int argc, char *argv[]) {
    const int no_employees = 10;
    Database people;
    init_database (people, no_employees);
    print_database(people, no_employees);
    sort(people, no_employees, comp_employee, swap_employee);
    print_database(people, no_employees);
    return 0; 
}
$ gcc main.cc
$ ./a.out
$