2012-05-01 129 views
3

我試圖在Ubuntu 11.04中編譯一個在Windows中運行良好的程序,但它給出了上述錯誤。我對導致錯誤的行添加了註釋。下面的代碼:C錯誤:數組類型具有不完整的元素類型

route_input() { 
    int num_routes;//Variable to act as the loop counter for the loop getting route details 
    int x; 

    char route_id[3]; 
    char r_source[20]; 
    char r_destination[20]; 
    int r_buses; 



    printf("Please enter the number of routes used: \n"); 
    scanf("%d", &num_routes); 
    char routes_arr[num_routes][10];//An array to hold the details of each route 

    printf("\nNumber of routes is %d\n", num_routes); 

    struct route r[num_routes];//An array of structures of type route (This line causes the error) 

    fflush(stdin); 

    for (x = num_routes; x > 0; x--) { 
     printf("\nEnter the route number: "); 
     scanf("%s", r[x].route_num); 
     printf("Route number is %s", r[x].route_num); 


     printf("\nEnter the route source: "); 
     fflush(stdin); 
     scanf("%s", r[x].source); 
     printf("Source = %s", r[x].source); 


     printf("\nEnter the route destination: "); 
     fflush(stdin); 
     gets(r[x].destination); 
     printf("Destination = %s", r[x].destination); 

     printf("\nEnter the number of buses that use this route: "); 
     scanf("%d", &r[x].num_of_buses); 
     printf("Number of buses = %d", r[x].num_of_buses); 


    } 

    for (x = num_routes; x > 0; x--) { 
     printf("\n\n+++Routes' Details+++\nRoute number = %s, Source = %s, Destination = %s, Number of buses for this route = %d\n", r[x].route_num, r[x].source, r[x].destination, r[x].num_of_buses); 
    } 

} 
+8

不要'fflush(stdin)',這是未定義的行爲。 – ouah

+0

http://stackoverflow.com/questions/2274550/gcc-array-type-has-incomplete-element-type – deebee

+4

什麼是'struct route',它在哪裏定義? – Mat

回答

0

據我所知,C(至少GCC不會)不會讓你有變量數組索引,這就是爲什麼它產生的錯誤。改爲嘗試一個常數。

它不具有多維陣列發生,因爲行是在沒有多暗淡陣列complusary但在單暗淡陣列數組索引的情況下,必須是可變的。

一些編譯器確實允許這樣的行爲,這就是爲什麼它不能在Windows產生錯誤。

+0

如果這是問題,'char routes_arr [num_routes] [10]'也會失敗。所以他可能使用了c99編譯器。 – ugoren

+0

正如我所說multidim數組的行變量是可選的,據我所知ubuntu11.04 GCC確實給出了這個錯誤,它不是基於C99 –

+0

我的觀點是,他顯然使用支持變量作爲維度的編譯器(或'routes_arr '會失敗)。所以他定義'r'的問題不可能是你說的。 – ugoren

2

您需要包含定義struct route的頭文件。
我不確定這是哪個頭,它可能會在Linux和Windows之間不同。

在Linux中,net/route.h定義了struct rtentry,這可能是您需要的。因爲你有struct route一個不完整的申報

5

錯誤消息造成的。即某處你有一條線說

struct route; 

沒有指定什麼是結構。這是完全合法的,並且允許編譯器在知道結構存在之前知道結構存在。這允許它爲不透明類型和前向聲明定義類型爲struct route的項的指針。

然而,由於它需要知道該結構的大小來計算的所需陣列中的存儲器的量,並從索引計算偏移編譯器不能使用不完整的類型作爲元素的數組。

我會說你忘記了包含定義你的路由結構的頭文件。另外,Ubuntu可能在其庫中有一個不透明的類型叫做struct route,所以你可能必須重新命名你的結構以避免衝突。

+0

感謝您的幫助。我包含了包含路由結構定義的頭文件,並且它正常工作。 – diamondtrust66

相關問題