簡體   English   中英

編譯錯誤我想不通

[英]Compile error i can't figure out

當我嘗試使用某個結構時,我得到一個“'數據'的存儲大小未知”。

代碼:

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:

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

map.c:

    void drawData(FILE* vectors)
    {

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

有任何想法嗎?

這是因為在編譯 map.c 時,編譯器看不到 IoHelper.c 中結構的完整定義。

您可能只包含了 IoHelper.h,它有(不完整的)聲明,而不是定義。

因此,除非您

  • 包括 IoHelper.c(壞主意)
  • 將結構定義放入 IoHelper.h
  • 在 map.c 和 malloc 中聲明指向結構的指針。

假設 map.c 不包括 IoHelper.c,它只看到 typedef 文件但沒有看到 struct.Value 的聲明因為它沒有看到聲明,所以它無法弄清楚結構有多大,因此編譯錯誤。

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不完整的類型 這意味着它沒有完全定義,所以它的大小是未知的。 在這種情況下,它根本沒有定義,只是聲明了。

您需要為聲明此類型變量的代碼提供一個定義,例如在您的 function drawData

您可以對不完整類型執行的操作包括將其用作指針或引用類型的基礎,並將其用作 function 聲明中的結果或按值參數類型(就像您所做的那樣)。 但是你不能做任何需要知道大小的事情。 聲明變量需要知道大小。

干杯&hth.,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM