简体   繁体   中英

“void value not ignored as it ought to be” in exercise from “Learn C the Hard Way”

So I'm following this lesson from the Learn C the Hard Way tutorials and I'm a little stuck.

When I try to compile I get this:

cc -Wall -g    ex19.c object.o   -o ex19
ex19.c:81:2: warning: initialization from incompatible pointer type [enabled by default]
ex19.c:81:2: warning: (near initialization for 'RoomProto.move') [enabled by default]
ex19.c: In function 'Map_move':
ex19.c:91:7: error: void value not ignored as it ought to be
ex19.c: At top level:
ex19.c:140:2: warning: initialization from incompatible pointer type [enabled by default]
ex19.c:140:2: warning: (near initialization for 'MapProto.move') [enabled by default]
make: *** [ex19] Error 1

From what I understand, this error occurs b/c the function returns next when it expects a void return type. But I thought void *foo() meant returning a void pointer.

Here's the code:

Object RoomProto = {
    .move = Room_move,
    .attack = Room_attack
};

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

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

Room Struct:

struct Room {
    Object proto;

    Monster *bad_guy;

    struct Room *north;
    struct Room *south;
    struct Room *east;
    struct Room *west;
};

How can I fix this error? I tried to change the return type, but ended up getting another error instead.

As far as I can tell, I copied the code in the tutorial char for char, so unless I made a mistake, the tutorial is incorrect.

@MattMcNabb helped me find the answer

Line 13 in object.h file was:

void *(move) (void *self, Direction direction);

instead of:

void *(*move) (void *self, Direction direction);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM