简体   繁体   English

警告:带有值的“返回”,在函数中返回void return next; ex19.c:95:10:错误:无效值不应该被忽略,因为它应该被忽略

[英]warning: ‘return’ with a value, in function returning void return next; ex19.c:95:10: error: void value not ignored as it ought to be

I am going through the Learn Code the hard way book and stuck on exercise 19 http://c.learncodethehardway.org/book/ex19.html . 我正在艰难地学习《学习代码》,并坚持练习19 http://c.learncodethehardway.org/book/ex19.html I got desperate, copied and pasted the code, yet still I am getting errors: 我不顾一切,复制并粘贴了代码,但是仍然出现错误:

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]
 }

This is what I got: 这就是我得到的:

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;
}

This line: 这行:

void Room_move(void *self, Direction direction)

should be: 应该:

void *Room_move(void *self, Direction direction)

This is the case on the page you linked, so you messed up while copy/pasting the code. 在您链接的页面上就是这种情况,因此在复制/粘贴代码时您一团糟。

You're missing up a pointer variable while specifying the return type for Room_move() function. 在指定Room_move()函数的返回类型时,您会丢失一个指针变量。 You need to use 您需要使用

void* Room_move(void *self, Direction direction)

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

void* Room_move() makes the return type as void * , but void Room_move() essentially means a void return. void* Room_move()将返回类型设为void * ,但void Room_move()本质上意味着返回void Side effect: 副作用:

  1. warning: 'return' with a value, in function returning void 警告:“返回”一个值,函数返回void

  2. error: void value not ignored as it ought to be 错误:空值不应该被忽略

So, as you can see, a void and void* are not the same. 所以,你可以看到,一个voidvoid*一样的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有忽略空值,因为它应该在 C 编程中 - Error: Void value not ignored as it ought to be in C programming 错误:C编程中应忽略的空值 - error: Void Value not ignored as it ought to be, C programming C编程中的错误“无效值不应该被忽略” - Error “void value not ignored as it ought to be” in C programming 返回一个int的函数上的C“无效值不应该被忽略”错误 - C “void value not ignored as it ought to be” error on a function that returns an int 警告:取消引用“void *”指针[默认启用]和错误:没有忽略无效值,因为它应该有助于c - warning: dereferencing ‘void *’ pointer [enabled by default] and error: void value not ignored as it ought to be help c 取消引用 function 指向 function 的指针返回 void 会引发错误: void 值没有被忽略,因为它应该被忽略 - dereferencing function pointer to function returning void throws error: void value not ignored as it ought to be 我有一个警告和错误,值无效; 警告:函数返回void,错误:void值未忽略 - I have a warning and error with void value; warning:in function returning void, error: void value not ignored 在C中获得“无效值不应该被忽略” - getting “void value not ignored as it ought to be” in C MINGW编译错误:无法忽略void值,因为它应该是 - MINGW compile error: void value not ignored as it ought to be C 警告“返回”没有值,在函数中返回非空 - C warning 'return' with no value, in function returning non-void
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM