Thread: Cross-Referencing Classes

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Cross-Referencing Classes

    I have two classes: mRoom and mDoor. mRoom has an array of 10 mDoors in it, and mDoor has 2 pointers which hold the adjacent mRooms. I can't seem to get this into code.

    NOTE: the main cpp file is just has the #include and a blank main function, with a return 0 statement.

    Here's the header file:
    Code:
    #ifndef MSOLVE_H_
    #define MSOLVE_H_
    
    class mRoom
    {
    private:
    	mDoor  exits[10];
    public:
    	mRoom( );
    	~mRoom( );
    };
    
    class mDoor
    {
    private:
    	mRoom * adjRooms[2];
    public:
    	mDoor( );
    	~mDoor( );
    };
    
    #endif
    And here are the errors:
    Code:
    --------------------Configuration: RoomMazeSol - Win32 Debug--------------------
    Compiling...
    main.cpp
    c:\c++\roommazesol\msolve.h(7) : error C2146: syntax error : missing ';' before identifier 'exits'
    c:\c++\roommazesol\msolve.h(7) : error C2501: 'mDoor' : missing storage-class or type specifiers
    c:\c++\roommazesol\msolve.h(7) : error C2501: 'exits' : missing storage-class or type specifiers
    Error executing cl.exe.
    
    RoomMazeSol.exe - 3 error(s), 0 warning(s)
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59

    Talking

    Code:
    class mRoom;
    
    class mDoor
    {
    private:
    	mRoom * adjRooms[2];
    public:
    	mDoor( );
    	~mDoor( );
    };
    
    class mRoom
    {
    private:
    	mDoor  exits[10];
    public:
    	mRoom( );
    	~mRoom( );
    };

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is the problem. You declare mRoom then mDoor. Your mRoom contains mDoor information but the compiler doesn't know about mDoor yet. Your mDoor is fine because above it is the mRoom declaration. So above your mRoom you need a forward declaration that will tell the compiler "There is in fact a mDoor class". It will look like this.

    Code:
    class mDoor;

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thanks. I got it to work using Pioneers method. MrWizards didn't.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Oops, because you aren't using a pointer in your mRoom class ( i.e. you have mDoor exits[10] not mDoor *exits[10] ) it will not work the way I suggested. I just spit the answer out a little too quickly I guess.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Minimax, URGENT :(
    By Dark-MX0Z in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 06:29 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. Referencing Classes?
    By JaWiB in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 02:16 AM