简体   繁体   English

如何在头文件中定义将在实现文件中定义的 ADT 结构

[英]How to define an ADT struct in header file that will be defined in implementation file

I have a header file that declares an ADT modeling a tuple.我有一个头文件声明一个 ADT 建模一个元组。 The header contains a declaration of the struct as typedef struct Tuple * Tuple;标头包含结构声明为typedef struct Tuple * Tuple; The corresponding implementation file defines this as相应的实现文件将其定义为

struct Tuple
{
    int x;
    int y;
    int z;
    int w;
};

The problem is that I want to import just the header file in client code (such as a test file or a file containing main).问题是我只想导入客户端代码中的头文件(例如测试文件或包含 main 的文件)。 I don't want to include the.c implementation file.我不想包含.c 实现文件。 Not doing so however results in an error message when attempting code from client code such as:但是,如果不这样做,则在尝试从客户端代码中尝试代码时会出现错误消息,例如:

Tuple tuple = ( Tuple ) calloc( 1, THREE_TUPLE_COORDINATES * sizeof( Tuple ) );
tuple->x ;

that reads as follows: error: dereferencing pointer to incomplete type 'struct Tuple'内容如下: error: dereferencing pointer to incomplete type 'struct Tuple'

Is it possible leave leave the struct incomplete/undefined in the header and then define it in the implementation file or is this not done/not best practice?是否可以在标头中留下不完整/未定义的结构,然后在实现文件中定义它,或者这不是完成/不是最佳实践?

error: dereferencing pointer to incomplete type 'struct Tuple'

Is it possible leave leave the struct incomplete/undefined in the header and then define it in the implementation file or is this not done/not best practice?是否可以在标头中留下不完整/未定义的结构,然后在实现文件中定义它,或者这不是完成/不是最佳实践?

You cannot use an abstract / incomplete data type in any context where the actual definition matters.您不能在实际定义很重要的任何上下文中使用抽象/不完整的数据类型。 Such contexts include declaring instances, accessing members, and determining the size.此类上下文包括声明实例、访问成员和确定大小。 How is the compiler supposed to know how much space to reserve?编译器应该如何知道要保留多少空间? Or where within that space to find the member with a particular name?或者在那个空间的什么地方可以找到具有特定名称的成员? Or whether such a member even exists?或者这样的成员是否存在?

You can provide functions for most of those things in the same translation unit(s) where the type is defined, and then let other translation units rely on those functions.可以在定义类型的同一翻译单元中为其中的大部分内容提供功能,然后让其他翻译单元依赖这些功能。 All instances obtained that way will need to be dynamically allocated, and all member accesses will incur the cost of a function call.以这种方式获得的所有实例都需要动态分配,并且所有成员访问都将产生函数调用的成本。 This sort of thing is sometimes done, but it's also often not done.这种事情有时做,但也经常不做 Do think carefully about what you want to gain by doing it.仔细考虑你想通过这样做获得什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c中的头文件中使用alies定义结构? - How to define struct with an alies in header file in c? 在单独的头文件中定义的结构 - A struct defined in a separate header file 如何在 header 文件中声明结构并在 .c 文件中定义它 - How do you declare a struct in a header file and define it in the .c file 标头中定义的结构未在C文件中重新协调? - Struct defined in header not reconized in C file? 如何在包含以C语言定义结构的头文件的文件中包括extern typdedef结构? - How to include extern typdedef struct in a file that contains the header file where the struct is defined in C? 如何在通用标头中类型定义实现定义的结构? - How do I typedef an implementation-defined struct in a generic header? 如何使用 C 中另一个 header 的某个 header 文件中定义的结构? - how to use a struct defined in a certain header file from another header in C? 标准库与C中用户定义的头文件(.h)及其实现文件(.c)有何不同? - How a standard library differ from user defined header file (.h) and its implementation file (.c) in C? 我可以使用头文件实现中不存在的结构吗? - Can I use a struct in a way that does not exist in the header file implementation? C编程。如果我需要在头文件(.h)中引用它的实例,如何在.c中隐藏结构的实现 - C programming. How to hide implementation of the struct in .c if I need to refer to its instance in header file (.h)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM