头文件中结构体互相引用的问题

先上代码看下错误的例子:

typedef struct _thread{
    int       id;                        /* friendly id               */
    pthread_t pthread;                   /* pointer to actual thread  */
    thpool_handle_t thpool_p;            /* access to thpool          */
} thread_t;


/* Threadpool
 * threadpool has many threads, and he should to access each threads, threads pointer array to save all threads pointer */
typedef struct _thpool{
    thread_t**   threads;                  /* pointer to threads        */
    volatile int num_threads_alive;      /* threads currently alive   */
    volatile int num_threads_working;    /* threads currently working */
    pthread_mutex_t  thcount_lock;       /* used for thread count etc */
    jobqueue*  jobqueue_p;               /* pointer to the job queue  */
} thpool_t, *thpool_handle_t;

编译提示:
./include/thread_pool.h:31:5: error: unknown type name ‘thpool_handle_t’

 

修改如下解决:

struct _thread;
struct _thpool;
typedef struct _thread thread_t;
typedef struct _thpool thpool_t, *thpool_handle_t;

typedef struct _thread{
    int       id;                        /* friendly id               */
    pthread_t pthread;                   /* pointer to actual thread  */
    thpool_handle_t thpool_p;            /* access to thpool          */
} thread_t;


/* Threadpool
 * threadpool has many threads, and he should to access each threads, threads pointer array to save all threads pointer */
typedef struct _thpool{
    thread_t**   threads;                  /* pointer to threads        */
    volatile int num_threads_alive;      /* threads currently alive   */
    volatile int num_threads_working;    /* threads currently working */
    pthread_mutex_t  thcount_lock;       /* used for thread count etc */
    jobqueue*  jobqueue_p;               /* pointer to the job queue  */
} thpool_t, *thpool_handle_t;

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以在需要使用结构体头文件使用 #include 指令引用另一个头文件,然后在需要使用结构体的地方使用结构体名称即可。例如: 在 a.h 头文件定义了一个结构体: struct student { char name[20]; int age; }; 在 b.h 头文件需要使用这个结构体,可以这样引用: #include "a.h" void print_student(struct student s); 然后在 b.c 文件实现 print_student 函数即可。 ### 回答2: 在一个头文件引用另一个头文件结构体可以通过以下步骤完成: 1. 首先,需要在被引用头文件定义结构体。比如,被引用头文件名为"header1.h",结构体名为"struct1",则在该头文件写入如下代码: ```c #ifndef HEADER1_H #define HEADER1_H struct struct1 { // 结构体定义 }; #endif ``` 2. 接下来,在引用结构体头文件,使用`#include`指令将被引用头文件包含进来。比如,引用头文件的文件名为"header2.h",则在该头文件写入如下代码: ```c #ifndef HEADER2_H #define HEADER2_H #include "header1.h" // 引用header1.h的结构体 // 在该头文件使用"header1.h"定义的结构体struct1 struct struct2 { struct struct1 s; // 引用结构体struct1 }; #endif ``` 3. 最后,在需要使用这两个结构体的源文件,分别包含引用结构体头文件和被引用结构体头文件即可。比如,在"main.c"源文件,写入如下代码: ```c #include "header2.h" int main() { struct struct2 s2; // 使用结构体s2和其引用结构体s ... return 0; } ``` 通过以上步骤,就实现了在一个头文件引用另一个头文件结构体。 ### 回答3: 在一个头文件引用另一个头文件结构体需要遵循以下步骤: 1. 在引用头文件头文件,使用`#include`指令引入需要引用头文件。例如,如果需要引用头文件名为`header1.h`,则可以写入`#include "header1.h"`。 2. 在需要使用另一个头文件结构体头文件,使用`#include`指令引入另一个头文件。例如,如果需要使用的头文件名为`header2.h`,则可以写入`#include "header2.h"`。 3. 在需要使用另一个头文件结构体头文件,使用`typedef struct`语句来定义该结构体的别名。例如,如果需要使用的结构体名为`structName`,则可以写入`typedef struct structName {} aliasName;`。这样,就可以使用`aliasName`作为该结构体的别名。 4. 在需要使用另一个头文件结构体的源文件,使用`#include`指令引入头文件`header1.h`。 5. 在源文件即可通过使用别名`aliasName`来访问和使用另一个头文件结构体。 需要注意的是,为了避免重复引用和循环引用问题,应该在引用头文件的文件使用条件编译指令来防止重复引用。可以使用`#ifndef`、`#define`和`#endif`三个指令来实现。例如: ```c #ifndef HEADER1_H #define HEADER1_H #include "header2.h" // 头文件的内容 #endif ``` 这样,当引用头文件已经被引用过时,就不会再次引用,避免了重复引用问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值