不完整类型编译时,键入已定义

时间:2013-11-20 08:49:22

标签: c types struct compiler-errors header-files

所以我的头文件列在下面。出于某种原因,当我尝试编译时,我得到以下错误。

orders.h:39: error: field ‘category_transactions’ has incomplete type
orders.h:51: error: field ‘address’ has incomplete type
orders.h:52: error: field ‘successful_trans’ has incomplete type
orders.h:53: error: field ‘failed_trans’ has incomplete type

我已经查看过其他人的类似问题和回复。我认为应该根据我的typedef来定义它们。我究竟做错了什么?

注意:我没有尝试创建指向项目的指针,我正在尝试使用共享内存并且已被告知要避免指向该目的。我正在考虑尝试简单地使它们成为长度为1的所有数组,但我不确定这是否只是一个解决问题的方法,或者它是否真的会解决问题。

谢谢。

#ifndef ORDERS_H
#define ORDERS_H 

 #define MAX_TRANS 30 
 #define MAX_CATEGORY 4
 #define MAX_CUSTOMERS 30  
 #define MAX_CUSTOMER_ID 30 
 #define MAX_CUSTOMER_NAME 50 
 #define MAX_ITEM_NAME 100 
 #define MAX_CATEGORY_NAME 30
 #define MAX_ZIP 10 
 #define MAX_STATE 20 
 #define MAX_STREET_CITY 80  
 #define SEM_ORDER_LIST "/order@@list" 
 #define SEM_USER_DATABASE "/user@@db" 
 #define SEM_PENDING_LIST "/pend@@list"

 typedef struct Transaction Transaction; 
 typedef struct Item Item; 
 typedef struct Customer Customer; 
 typedef struct Mailing_Address Mailing_Address; 
 typedef struct Category Category;
 typedef struct Category_List Category_List;  
 typedef struct Customer_List Customer_List; 
 typedef struct Transaction_List Transaction_List; 



 struct Category{ 
    char category_name[MAX_CATEGORY_NAME]; 
    Transaction_List category_transactions; 
 }; 

 struct Category_List{ 
    unsigned int curr_length; 
    Category all_categories[MAX_CATEGORY]; 
 }; 

 struct Customer{ 
    char customer_name[MAX_CUSTOMER_NAME]; 
    char customer_id[MAX_CUSTOMER_ID];
    double  credit_balance; 
    Mailing_Address address; 
    Transaction_List successful_trans;
    Transaction_List failed_trans;  
 }; 

 struct Customer_List{ 
    unsigned int curr_length;  
    Customer all_customers[MAX_CUSTOMERS];
 };  

 struct Item{ 
    char item_name[MAX_ITEM_NAME]; 
    double price; 
    char category_name[MAX_CATEGORY_NAME]; 
 }; 

 struct Mailing_Address{ 
    char zipcode[MAX_ZIP]; 
    char state[MAX_STATE]; 
    char street_city[MAX_STREET_CITY]; 
 }; 

 struct Transaction{ 
    Item  object; 
    char customer_id[MAX_CUSTOMER_ID];  
 }; 

 struct Transaction_List{ 
    unsigned int curr_length;  
    Transaction all_transactions[MAX_TRANS];  
 };     

#endif

1 个答案:

答案 0 :(得分:3)

当您定义struct对象(如struct myStruct member;)时,编译器需要知道myStruct的定义才能分配存储空间。你只有typedef,它只是一个别名,并没有告诉你实际的结构。

如果您重新订购结构定义以确保在其他人使用时它们是可见的,那么您应该没问题。

 typedef struct Transaction Transaction; 
 typedef struct Item Item; 
 typedef struct Customer Customer; 
 typedef struct Mailing_Address Mailing_Address; 
 typedef struct Category Category;
 typedef struct Category_List Category_List;  
 typedef struct Customer_List Customer_List; 
 typedef struct Transaction_List Transaction_List; 

 struct Mailing_Address{
    char zipcode[MAX_ZIP];
    char state[MAX_STATE];
    char street_city[MAX_STREET_CITY];
 };

 struct Item{
    char item_name[MAX_ITEM_NAME];
    double price;
    char category_name[MAX_CATEGORY_NAME];
 };


 struct Transaction{
    Item  object;
    char customer_id[MAX_CUSTOMER_ID];
 };

 struct Transaction_List{
    unsigned int curr_length;
    Transaction all_transactions[MAX_TRANS];
 };


 struct Category{ 
    char category_name[MAX_CATEGORY_NAME]; 
    Transaction_List category_transactions; 
 }; 

 struct Category_List{ 
    unsigned int curr_length; 
    Category all_categories[MAX_CATEGORY]; 
 }; 

 struct Customer{ 
    char customer_name[MAX_CUSTOMER_NAME]; 
    char customer_id[MAX_CUSTOMER_ID];
    double  credit_balance; 
    Mailing_Address address; 
    Transaction_List successful_trans;
    Transaction_List failed_trans;  
 }; 

 struct Customer_List{ 
    unsigned int curr_length;  
    Customer all_customers[MAX_CUSTOMERS];
 };