我对c中的结构感到困惑。我正在尝试创建一个.h文件,其中包含我将使用的所有结构。我创建了structs.h
#include <ucontext.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct TCB_t;
typedef struct
{
struct TCB_t * next;
struct TCB_t * previous;
ucontext_t context;
int val;
}TCB_t;
我的TCB.h文件
#include "structs.h"
int count =0;
struct TCB_t *RunQ = NULL;
struct TCB_t *ptr = NULL;
void init_TCB (struct TCB_t *tcb, void *function, void *stackP, int stack_size, int *arg)
{
memset(tcb, '\0', sizeof(struct TCB_t));
getcontext(&tcb->context);
tcb->context.uc_stack.ss_sp = stackP;
tcb->context.uc_stack.ss_size = (size_t)stack_size;
makecontext(&tcb->context, function, 1, arg);
}
当我跑步时,我得到以下错误。
Description Resource Path Location Type
Field 'ss_size' could not be resolved TCB.h /projThree/src line 14 Semantic Error
Description Resource Path Location Type
Field 'ss_sp' could not be resolved TCB.h /projThree/src line 13 Semantic Error
Description Resource Path Location Type
Field 'uc_stack' could not be resolved TCB.h /projThree/src line 13 Semantic Error
Description Resource Path Location Type
Field 'uc_stack' could not be resolved TCB.h /projThree/src line 14 Semantic Error
Description Resource Path Location Type
Symbol 'NULL' could not be resolved TCB.h /projThree/src line 6 Semantic Error
Description Resource Path Location Type
Symbol 'NULL' could not be resolved TCB.h /projThree/src line 7 Semantic Error
如果我将struct fron structs.h移动到TCB.h,那么错误就会消失。为什么TCB.h不能访问structs.h中的结构,因为我包含了#34; structs.h&#34;在页面顶部?
答案 0 :(得分:1)
问题在于您已声明某处有struct TCB_t
,并且您已为无标签(匿名){{1}定义了名为typedef
的{{1}} }}类型,但您尚未定义类型TCB_t
。
struct
你需要写下这个:
struct TCB_t
或者这个:
struct TCB_t; // There is, somewhere, a type struct TCB_t
typedef struct // This is an anonymous struct, not a struct TCB_t
{
struct TCB_t * next;
struct TCB_t * previous;
ucontext_t context;
int val;
} TCB_t; // This is a typedef for the anonymous struct
最终都有typedef struct TCB_t TCB_t;
struct TCB_t
{
TCB_t *next; // Optionally struct TCB_t
TCB_t *previous; // Optionally struct TCB_t
ucontext_t context;
int val;
};
和普通类型typedef struct TCB_t
{
struct TCB_t *next;
struct TCB_t *previous;
ucontext_t context;
int val;
} TCB_t;
,这是struct TCB_t
的别名。
请注意,TCB_t
后缀正式保留供实现(编译器和支持库)使用。您可能会遇到自己使用它的问题(但是在更改名称之前可能不会很晚)。
编译错误的原因是编译器没有被告知struct TCB_t
包含的内容,因此您无法访问_t
成员,因此不能访问其中的字段struct TCB_t
成员。