Right now I'm working on a project for my course on system programming. We were asked to program an apartment selling platform, with Realtors and Customers. I'm working on Eclipse.
现在我正在为我的系统编程课程开展一个项目。我们被要求与房地产经纪人和客户一起编制公寓销售平台。我正在研究Eclipse。
Now, even though I didn't experience any problems like that in the past, one of my header files fails to recognize a typedef from a second header file.
现在,即使我在过去没有遇到任何问题,我的一个头文件也无法识别第二个头文件中的typedef。
Explanation: here are my files;
说明:这是我的文件;
Realtor.h
Realtor.h
#include "apartment.h"
#include "apartment_service.h"
#include "Report.h"
#include "Customer.h"
#include "mtm_ex2.h"
typedef struct realtor_t* Realtor;
While this is the second header file;
虽然这是第二个头文件;
Customer.h
Customer.h
#include "Report.h"
#include "Realtor.h"
#include "apartment.h"
#include "apartment_service.h"
#include "mtm_ex2.h"
typedef struct customer_t* Customer;
MtmErrorCode purchaseApartment (Customer customer, Realtor realtor,
ApartmentService service,
int apartment_id);
MtmErrorCode makeOffer (Customer customer, Realtor realtor, ApartmentService
service, int apartment_id, int new_price);
(the structs of customer_t and realtor_t are defined in the source files)
(customer_t和realtor_t的结构在源文件中定义)
For some reason, the function declarations in Customer.h give me the following error: "unknown type name 'Realtor'". This is really weird because the same functions use other typedefs like 'ApartmentService' from "apartment_service.h".
出于某种原因,Customer.h中的函数声明给出了以下错误:“未知类型名称'Realtor'”。这真的很奇怪,因为相同的函数使用其他typedef,例如“apartment_service.h”中的'ApartmentService'。
2 个解决方案
#1
2
You are including Customer.h in Realtor.h.
您在Realtor.h中包含Customer.h。
This is where the error is occurring. In Realtor.h, the typedef for Realtor
is not defined before Customer.h
这是发生错误的地方。在Realtor.h中,Realtor的typedef未在Customer.h之前定义
Remove the inclusion of Customer.h from Realtor.h. That should solve the problem for the code given.
从Realtor.h中删除Customer.h的包含。这应该解决给定代码的问题。
#2
0
You simply shouldn't do this. You should include these include files in your code files, not your include files.
你根本不应该这样做。您应该在代码文件中包含这些包含文件,而不是包含文件。
Having declarations like typedef struct customer_t* Customer;
in your header files is fine. You don't need such a declaration to know anything more as you are defining a pointer to some type. That is all the compiler needs to know for the prototypes you give.
像typedef struct customer_t * Customer这样的声明;在你的头文件中没问题。在定义指向某种类型的指针时,您不需要这样的声明来了解更多内容。这就是编译器需要知道的所有原型。
EDIT
编辑
Each module should only export what it makes available and the total should "build up" so there are preferably no circular dependencies. So Apartment is for example a base "class", Apartment Services uses Apartment; Customer is a base "class", and your program has functions for a Customer to buy an Apartment. Sometimes circuar references or forward references can't be prevented. In that case you should use "include guards" that ensure the contents of an include file is included only once.
每个模块应该只导出它所提供的内容,并且总数应该“建立”,因此最好没有循环依赖。所以公寓是一个基础“类”,公寓服务使用公寓;客户是一个基础“类”,您的程序具有客户购买公寓的功能。有时无法阻止循环引用或前向引用。在这种情况下,您应该使用“包含保护”,以确保包含文件的内容仅包含一次。
/* apartment.h */
#ifndef APT_INCLUDED
#define APT_INCLUDED
typedef struct Apartment* tApartmemt;
tApartment NewApartment(char * name);
#endif /*APT_INCLUDED*/
/* apartment_services.h */
#ifndef APTSVC_INCLUDED
#define APTSVC_INCLUDED
#include "apartment.h"
typedef struct ApartmentService* tAptSvc;
tAptSvc NewAptSvc(tApartment apartment, char *svcname);
#endif /*APTSVC_INCLUDED*/
Taking a good look at who needs to know about what may simplify your include structure too, for example, I doubt that makeOffer
should be part of the customer module and as a result the module should not need to include apartment or realtor; rather, it is the realtor that makes the offer. I also note that your includes seem a bit in random order. Anyway, using the include guards can prevent circular includes and should solve you original problem. I hope this helps a bit.
仔细看看谁需要知道什么可以简化你的包含结构,例如,我怀疑makeOffer应该是客户模块的一部分,因此模块不应该包括公寓或房地产经纪人;相反,是提供报价的房地产经纪人。我还注意到你的包含似乎有点随机。无论如何,使用包含警卫可以防止循环包含,并应解决您原来的问题。我希望这个能有一点帮助。
#1
2
You are including Customer.h in Realtor.h.
您在Realtor.h中包含Customer.h。
This is where the error is occurring. In Realtor.h, the typedef for Realtor
is not defined before Customer.h
这是发生错误的地方。在Realtor.h中,Realtor的typedef未在Customer.h之前定义
Remove the inclusion of Customer.h from Realtor.h. That should solve the problem for the code given.
从Realtor.h中删除Customer.h的包含。这应该解决给定代码的问题。
#2
0
You simply shouldn't do this. You should include these include files in your code files, not your include files.
你根本不应该这样做。您应该在代码文件中包含这些包含文件,而不是包含文件。
Having declarations like typedef struct customer_t* Customer;
in your header files is fine. You don't need such a declaration to know anything more as you are defining a pointer to some type. That is all the compiler needs to know for the prototypes you give.
像typedef struct customer_t * Customer这样的声明;在你的头文件中没问题。在定义指向某种类型的指针时,您不需要这样的声明来了解更多内容。这就是编译器需要知道的所有原型。
EDIT
编辑
Each module should only export what it makes available and the total should "build up" so there are preferably no circular dependencies. So Apartment is for example a base "class", Apartment Services uses Apartment; Customer is a base "class", and your program has functions for a Customer to buy an Apartment. Sometimes circuar references or forward references can't be prevented. In that case you should use "include guards" that ensure the contents of an include file is included only once.
每个模块应该只导出它所提供的内容,并且总数应该“建立”,因此最好没有循环依赖。所以公寓是一个基础“类”,公寓服务使用公寓;客户是一个基础“类”,您的程序具有客户购买公寓的功能。有时无法阻止循环引用或前向引用。在这种情况下,您应该使用“包含保护”,以确保包含文件的内容仅包含一次。
/* apartment.h */
#ifndef APT_INCLUDED
#define APT_INCLUDED
typedef struct Apartment* tApartmemt;
tApartment NewApartment(char * name);
#endif /*APT_INCLUDED*/
/* apartment_services.h */
#ifndef APTSVC_INCLUDED
#define APTSVC_INCLUDED
#include "apartment.h"
typedef struct ApartmentService* tAptSvc;
tAptSvc NewAptSvc(tApartment apartment, char *svcname);
#endif /*APTSVC_INCLUDED*/
Taking a good look at who needs to know about what may simplify your include structure too, for example, I doubt that makeOffer
should be part of the customer module and as a result the module should not need to include apartment or realtor; rather, it is the realtor that makes the offer. I also note that your includes seem a bit in random order. Anyway, using the include guards can prevent circular includes and should solve you original problem. I hope this helps a bit.
仔细看看谁需要知道什么可以简化你的包含结构,例如,我怀疑makeOffer应该是客户模块的一部分,因此模块不应该包括公寓或房地产经纪人;相反,是提供报价的房地产经纪人。我还注意到你的包含似乎有点随机。无论如何,使用包含警卫可以防止循环包含,并应解决您原来的问题。我希望这个能有一点帮助。