Implicit declaration, yet works fine

Posted by Zeno on Sun 06 Jun 2004 06:56 AM — 4 posts, 9,946 views.

USA #0
This was my easy way to log shards to a file:


act_obj.c: In function `get_obj':
act_obj.c:171: warning: implicit declaration of function `shardmon'



sprintf(buf, "%s(%d) got a shard from in %s(%d) in room %d",
 ch->name, IS_NPC(ch) ? ch->pIndexData->vnum : 0, container->name, container->pIndexData->vnum, ch->in_room->vnum );
shardmon( ch, buf );



void shardmon( CHAR_DATA *ch, char *argument )
{
    if ( argument[0] == '\0' )
        return;
    append_file( ch, SHARDMON_FILE, argument );
    return;
}


Yet it appends fine, and with that warning. I always want to keep the compiling warning-free. Yes, I know its not a decent way to log file, but it was a quick fix, since I forgot that the shard_monitor channel didn't append to log. Hm, how about explaining why it said that warning? Heh, never did learn *nix or C through a class. Just through testing.
USA #1
That means that the compiler hasn't seen the function declaration yet. However, since gcc is fairly lax on default settings, it'll let you get away with it. To fix it, all you need to do is add:

void shardmon( CHAR_DATA *ch, char *argument );

Somewhere above wherever it is first placed. This is called a 'forward declaration' - you're telling the compiler something exists but that you will define it later.
USA #2
Ah, totally forgot about that. I was thinking that the function was not going to work.
USA #3
If you have time, I would suggest that you compile with g++ and not gcc. You'll have to adapt many things, e.g. you can't use the keywords 'new' and 'class', but in the end your code will be much more rigorous and just all around cleaner.