2015-03-13 74 views
1

我正在通過學​​習代碼困難的方式預訂並停留在練習19 http://c.learncodethehardway.org/book/ex19.html。我絕望了,複製和粘貼代碼,但仍然我得到錯誤:警告:'返回'一個值,在函數返回void返回下一個; ex19.c:95:10:錯誤:無效值不會被忽略,因爲它應該是

cc -Wall -g ex19.c object.o -o ex19 

ex19.c: In function ‘Room_move’: 

ex19.c:65:5: warning: ‘return’ with a value, in function returning void 
    return next; 

    ^
ex19.c: In function ‘Map_move’: 

ex19.c:95:10: error: void value not ignored as it ought to be 
    next = location->_(move)(location, direction); 

     ^
ex19.c: In function ‘Room_attack’: 

ex19.c:145:5: warning: initialization from incompatible pointer type 
    .move = Map_move, 

    ^
ex19.c:145:5: warning: (near initialization for ‘MapProto.move’) 

ex19.c:199:5: warning: ‘main’ is normally a non-static function [-Wmain] 
int main(int argc, char *argv[]) 

    ^
ex19.c:214:1: error: expected declaration or statement at end of input 
} 
^ 
ex19.c:214:1: error: expected declaration or statement at end of input 

ex19.c:214:1: warning: control reaches end of non-void function [-Wreturn-type] 
} 

這是我得到:

void Room_move(void *self, Direction direction) 
{ 
    Room *room = self; 
    Room *next = NULL; 

    if(direction == NORTH && room->north) { 
     printf("You go north, into:\n"); 
     next = room->north; 
    } else if(direction == SOUTH && room->south) { 
     printf("You go south, into:\n"); 
     next = room->south; 
    } else if(direction == EAST && room->east) { 
     printf("You go east, into:\n"); 
     next = room->east; 
    } else if(direction == WEST && room->west) { 
     printf("You go west, into:\n"); 
     next = room->west; 
    } else { 
     printf("You can't go that direction."); 
     next = NULL; 
    } 

    if(next) { 
     next->_(describe)(next); 
    } 

    return next; 
} 

void *Map_move(void *self, Direction direction) 
{ 
    Map *map = self; 
    Room *location = map->location; 
    Room *next = NULL; 

    next = location->_(move)(location, direction); 

    if(next) { 
     map->location = next; 
    } 

    return next; 
} 

int main(int argc, char *argv[]) 
{ 
    // simple way to setup the randomness 
    srand(time(NULL)); 

    // make our map to work with 
    Map *game = NEW(Map, "The Hall of the Minotaur."); 

    printf("You enter the "); 
    game->location->_(describe)(game->location); 

    while(process_input(game)) { 
    } 

    return 0; 
} 
+0

當然練習1到18有一些關於如何返回工作 – 2015-03-13 07:39:06

回答

3

這條線:

void Room_move(void *self, Direction direction) 

應該是:

void *Room_move(void *self, Direction direction) 

這種情況您鏈接的頁面,所以在複製/粘貼代碼時混亂了。

+0

從複製和粘貼代碼中得到錯誤後,可能在本網站進行研究,我將void * Room_move(void * self,Direction direction)更改爲void Room_move (void * self,方向方向),現在我用來取得的錯誤被另一個取代 – codeing 2015-03-13 18:51:01

1

您在指定Room_move()函數的返回類型時缺少指針變量。您需要使用

void* Room_move(void *self, Direction direction) 

而不是void Room_move(void *self, Direction direction)

void* Room_move()使返回類型爲void *,但void Room_move()本質上意味着void返回。副作用:

  1. warning: ‘return’ with a value, in function returning void

  2. error: void value not ignored as it ought to be

所以,你可以看到,一個voidvoid*相同。

相關問題