2014-11-14 43 views
0

我對c中的結構感到困惑。我正在嘗試創建一個包含我將使用的所有結構的.h文件。我創建structs.hC和.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中的結構體,因爲我在頁面頂部包含了「structs.h」?

+0

函數定義在.h文件中? – Gopi

+0

取決於'structs.h'的位置。也許試試'#include '。另外,函數定義應該放入'.c'文件中。 – bwinata

+0

我有點困惑。我正在使用c來參加我正在上課的課程。他讓我們使用.h文件中的函數定義 – Aaron

回答

1

麻煩的是,你已經宣稱有一個struct TCB_t的地方,以及您所定義的名稱TCB_t一個typedef的無標籤(匿名)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; 

兩端瓦特ith struct TCB_t和一個普通類型TCB_t,它是struct TCB_t的別名。

當心,_t後綴被官方保留供實施(編譯器和支持庫)使用。你可能會遇到自己使用它的問題(但可能不會改變名稱後不舒服)。

併爲您的編譯錯誤的原因是編譯器沒有被告知什麼struct TCB_t包含,所以你不能訪問它的context成員,因此不context成員中的字段。