简体   繁体   English

编译错误我想不通

[英]Compile error i can't figure out

im getting a "storage size of 'data' isn't known" when i try to use a certain struct.当我尝试使用某个结构时,我得到一个“'数据'的存储大小未知”。

Code:代码:

ioHelper.h: ioHelper.h:

    #ifndef IOHELPER_H_
    #define IOHELPER_H_

    typedef struct fileValues data;

    struct fileValues ioInput(FILE* file,int dim,int sign);

    #endif /* IOHELPER_H_ */

ioHelper.c: ioHelper.c:

    struct fileValues
    {
int dim;
char sign;
double x;
double y;
    };

map.c: map.c:

    void drawData(FILE* vectors)
    {

double paramLoc [MAX_DIMENSION];
char sign;
(this is where i get the error) struct fileValues data;
    ...
    }

any ideas?有任何想法吗?

This is because when compiling map.c the compiler can't see the full definition of the struct in IoHelper.c.这是因为在编译 map.c 时,编译器看不到 IoHelper.c 中结构的完整定义。

You probably only included IoHelper.h, which has the (incomplete) declaration, not the definition.您可能只包含了 IoHelper.h,它有(不完整的)声明,而不是定义。

So you cannot declare a variable of that struct type inside of map.c unless you因此,除非您

  • include IoHelper.c (BAD IDEA)包括 IoHelper.c(坏主意)
  • put the struct definition in IoHelper.h将结构定义放入 IoHelper.h
  • declare a pointer to the struct in map.c and malloc it.在 map.c 和 malloc 中声明指向结构的指针。

Assuming that map.c is not including IoHelper.c, it only sees the typedef but not the declaration of struct fileValues.假设 map.c 不包括 IoHelper.c,它只看到 typedef 文件但没有看到 struct.Value 的声明Because it didn't see the declaration, it can't figure out how big the structure is, hence the compile error.因为它没有看到声明,所以它无法弄清楚结构有多大,因此编译错误。

Normally you would declare a struct in a header file - move it from iohelper.c to iohelper.h and map.c should now compile. Normally you would declare a struct in a header file - move it from iohelper.c to iohelper.h and map.c should now compile.

data is an incomplete type . data不完整的类型 which means that it isn't fully defined, so its size is not known.这意味着它没有完全定义,所以它的大小是未知的。 in this case, it's not defined at all, just declared.在这种情况下,它根本没有定义,只是声明了。

you need to make a definition available to code that declares a variable of this type, such as in your function drawData .您需要为声明此类型变量的代码提供一个定义,例如在您的 function drawData

things you can do with an incomplete type include using it as base for a pointer or reference type, and using it as result or by-value argument type in a function declaration (as you did).您可以对不完整类型执行的操作包括将其用作指针或引用类型的基础,并将其用作 function 声明中的结果或按值参数类型(就像您所做的那样)。 but you cannot do anything that requires knowing the size.但是你不能做任何需要知道大小的事情。 declaring a variable requires knowing the size.声明变量需要知道大小。

cheers & hth.,干杯&hth.,

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM