首页 > 解决方案 > 变量集但未使用

问题描述

编译时,我收到以下错误:

roadplan.c:64:11: error: variable 'thisRoad' set but not used [-Werror=unused-but-set-variable]
     Road *thisRoad = NULL;

这里感兴趣的行是(这篇文章底部的完整功能):

Road *thisRoad = NULL;
----
/*    Create new road structure using new_road() function*/
        newRoad = new_road(org, dest, length);
        /*    Add it to the list of roads of the origin city.*/
        while (org -> roads -> next != NULL){
            thisRoad = org -> roads -> next;
        }
        thisRoad = newRoad;

当做明显的(删除声明)时,我自然会得到未声明变量的错误。此外,我尝试更改值的名称。我已经没有关于如何解决这个问题的想法了,请帮忙!

static City *create_map (FILE *data_file)
{
    int num_of_cities, length, num_of_roads, i;
    char org_name[MAX_STRING_LENGTH + 1], dest_name[MAX_STRING_LENGTH + 1];
    
    City *map = NULL;
    City *org = map;
    City *dest = map;
    Road *newRoad = NULL;
    Road *thisRoad = NULL;

    /* Read in city-names   */
    fscanf (data_file, "%d", &num_of_cities);

    for (i = 0; i < num_of_cities; i++)
    {
        char city_name[MAX_STRING_LENGTH + 1];
        City *city;
        City *c;

        fscanf (data_file, "%s", city_name);

        if (find_city (map, city_name) != NULL)
        {
            fprintf (stderr, "City %s already on the map\n", city_name);
            delete_map (map);
            exit (EXIT_FAILURE);
        }

        city = new_city (safe_strdup (city_name));

        if (map == NULL)
            /* This is the first city of the map */
            map = city;
        else {
            /* Find last of city list */
            c = map;
            while (c->next) c = c->next;
            /* And append new city there */
            c -> next = city;
        }
    }

         /* Read number of roads*/
    fscanf (data_file, "%d", &num_of_roads);
         /* For each road:*/
    for (i = 0; i<num_of_roads; i++) {
        /*    Read origin city name, destination city name and length*/
        fscanf(data_file, "%s %s %d", org_name, dest_name, &length);
        /*    Find pointers to orgin city structure and destination city structure*
         *    using the function find_city()*/
        org = find_city(map, org_name);
        dest = find_city(map, dest_name);
        /*    Create new road structure using new_road() function*/
        newRoad = new_road(org, dest, length);
        /*    Add it to the list of roads of the origin city.*/
        while (org -> roads -> next != NULL){
            thisRoad = org -> roads -> next;
        }
        thisRoad = newRoad;
    }
    return map;
}

标签: c

解决方案


您对 进行了多次分配thisRoad,但实际上从未从中读取任何这些值。这会提示您了解此代码的真正问题:

        while (org -> roads -> next != NULL){
            thisRoad = org -> roads -> next;
        }
        thisRoad = newRoad;

,这似乎是您唯一使用的地方thisRoad。编译器没有足够的模式识别功能来识别您实际尝试在那里执行的操作,但它可以观察到有多个分配给thisRoad,但从来没有任何读取。它假设如果你分配一个值,那是因为至少有时你会想要读回它。

在这种情况下,该循环将运行零次或无限次,因为循环体不执行任何会改变循环条件结果的操作。看起来您可能正在尝试将元素附加到链表,这更像是这样:

    Road *thisRoad = org->roads;     // Style: use the narrowest sufficient scope

    while (thisRoad->next != NULL) { // uses the value of thisRoad
        thisRoad = thisRoad->next;   // uses and modifies the value of thisRoad
    }
    thisRoad->next = newRoad;        // uses the value of thisRoad

推荐阅读