undefined C struct forward declaration

后端 未结 4 1596
感情败类
感情败类 2020-12-05 14:39

I have a header file port.h, port.c, and my main.c

I get the following error: \'ports\' uses undefined struct \'port_t\'

I thought as I have declared the str

4条回答
  •  猫巷女王i
    2020-12-05 15:19

    If you want to hide the internal data of the port_t structure you can use a technique like how the standard library handles FILE objects. Client code only deals with FILE* items, so they do not need (indeed, then generally can't) have any knowlege of what is actually in the FILE structure. The drawback to this method is that the client code can't simply declare a variable to be of that type - they can only have pointers to it, so the object needs to be created and destroyed using some API, and all uses of the object have to be through some API.

    The advantage to this is that you have a nice clean interface to how port_t objects must be used, and lets you keep private things private (non-private things need getter/setter functions for the client to access them).

    Just like how FILE I/O is handled in the C library.

提交回复
热议问题