Thread: Help with Steve Summit's tutorial

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    169

    Help with Steve Summit's tutorial

    Hi there,
    Just finished his beginner's c tutorials, which were very helpful, and got on to intermediate.
    I am now trying my luck with the second week's Assignment (Link) but running into some trouble: I do what is asked in the first exercise, without understanding much of what I am doing, but no matter what I try I can't compile it. The source, by the way, was provided. Heres a screen of the problem: Screenshot
    So what am I missing? has anyone succeeded in this exercise? Can it be a problem with the source?

    - I use cygwin

    Thanks.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I do C++, but I have seen this error before. It says actor is undifined, meaning either you have not decared it in main or you have forgoten to extern link it to main from another file. If actor is in another header file, make sure it is included in the main file. Another alternative is to use the debugger to step through the complation process and see what is going wrong. But if you have delcared actor in main, is is spelt correctly? Remeber both C and C++ is case sensitive. If this is not the answer, perhaps somthing is wrong with the code in the tutorial

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    169
    Yes, the actor structure is declared in another header file, but is linked and included.
    Code:
    $ make -f makefile.unix
    gcc    -c -o commands.o commands.c
    gcc    -c -o getliner.o getliner.c
    gcc    -c -o getwords.o getwords.c
    gcc    -c -o main.o main.c
    main.c: In function `main':
    main.c:34: error: `actor' undeclared (first use in this function)
    main.c:34: error: (Each undeclared identifier is reported only once
    main.c:34: error: for each function it appears in.)
    make: *** [main.o] Error 1
    Though it still says something is wrong.
    If anyone managed this exercise Id like to know if he ran into the same problem please.

    Thanks.
    Last edited by glo; 05-16-2006 at 03:56 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you #include-ing the header file it's declared in? You have to.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    169
    Yes, it is included:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "game.h"
    #include "utils.h"
    #include "defs.h"
    This is how it is declared in game.h:
    Code:
    struct actor
        {
        struct room *location;
        struct object *contents;    /* possessions */
        };
    And this is the makefile:
    Code:
    CC = gcc
    
    OBJS = commands.o getliner.o getwords.o main.o object.o parser.o rooms.o
    
    game: $(OBJS)
    	$(CC) -o $@ $(OBJS)
    
    commands.o: commands.c game.h defs.h
    getliner.o: getliner.c utils.h
    getwords.o: getwords.c utils.h
    main.o: main.c game.h utils.h
    object.o: object.c game.h defs.h
    parser.o: parser.c defs.h game.h utils.h
    rooms.o: rooms.c game.h defs.h

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you using the keyword 'struct' before 'actor' in your usage of it? C isn't C++. This is legal:
    Code:
    struct actor x;
    ...
    void foo( struct actor ); /* a function prototype */
    ...
    void foo( struct actor a ) /* a function definition... */
    {
        ...
    }
    This isn't:
    Code:
    actor x;
    ...
    foo( actor );
    ...
    foo( actor a )
    {
        ...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    169
    Quite interestingly there is a line using, what may seem, an ilegal use of the struct:
    Code:
    if(!readdatafile())
            exit(1);
    
        gotoroom(&actor, getentryroom());    /* put actor in initial room */
    Could this be it?

    Its definition, by the way, is:
    Code:
    int
    gotoroom(struct actor *actor, struct room *room)
    {
    actor->location = room;
    return TRUE;
    }
    Last edited by glo; 05-17-2006 at 04:58 AM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is 'actor' in that reference? Have you declared a variable instance prior to that called 'actor'? Something like...
    Code:
    struct actor actor;
    ...
    gotoroom( &actor, getentryroom( ) );

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    169
    Well there is this decleration in main.c:
    Code:
    static struct actor player;
    But the structure definition should be prior to it, it is included just before it.
    I hope Im getting it right, all I did was follow the instructions he provided and paste bits of code into source code files he provided as well. (you can check the exercise linked in the first post)

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be passing the variable 'player' then instead of 'actor'. This is why it's a bad idea to name your variable instances the same thing as the structure tag.
    Code:
    struct actor actor; /* bad */
    struct actor player; /* un-bad */
    Code:
    int
    gotoroom(struct actor *actor, struct room *room)
    {
    actor->location = room;
    return TRUE;
    }
    [/quote]

    Quzah.
    Last edited by quzah; 05-17-2006 at 06:41 AM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    169
    Woo! thanks!

    Apparently this was not the only problem with this exercise:
    "After adding all the new code, and any necessary prototype declarations to game.h, the program should again compile and run"
    Made me believe it should compile after following his instructions, but in reality, other than the error you mentioned, I also had to edit makefile.unix (this process wasnt tutored yet either).

    Anyway all is good now, thanks.
    If anyone ever comes across this tutorial make sure to fix these problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM