2012-05-01 99 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

相关问题