Thread: Object array. Probelm accessing methods

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Object array. Probelm accessing methods

    Hi, in the program I am writing I have placed several objects of the same class in an array. When I try and access a public method of one of the objects from a different .cpp file, it displays several error messages even though the object array is global. I have abridged versions of the files below. Any ideas?

    Code:
    // main .cpp file
    #include <iostream>
    #include "Room.h"
    
    using namespace std;
    
    CRoom Rooms[14]={1,2,3,4,5,6,7,8,9,10,11,12,13,14};
    
    void main()
    {
    
    	title();
    
    	Rooms[1].set_door(0,2,0,1);
    	Rooms[2].set_door(7,1,9,11);
    	Rooms[3].set_door(9,0,0,10);
    	Rooms[4].set_door(0,10,0,0);
    
    // ...etc etc
    
    // another .cpp file
    
    #include <iostream>
    #include "Player.h"
    
    using namespace std;
    
    CPlayer::~CPlayer()
    {
    
    }
    
    void CPlayer::move(int direction)
    {
         Rooms[1].set_door(1,2,3,4); 
    
    // errors occurs when this line is include e.g.
    // 'Rooms' : undeclared identifier
    // subscript requires array or pointer type
    // left of '.set_door' must have class/struct/union type
    
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In your other source file, put:
    extern CRoom Rooms[14];
    This lets the compiler know that Rooms has been declared and defined elsewhere.

    Also, 'void main' is bad, 'int main' is good. For a thorough explanation of this, consult the FAQ.

    Cheers

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    erm, I get less errors now. Here the code as it looks now:

    Code:
    #include <iostream>
    #include "Player.h"
    
    extern CRoom Rooms[14]; // newly added
    
    using namespace std;
    
    CPlayer::~CPlayer()
    {
    
    }
    
    void CPlayer::move(int direction)
    {
          Rooms[1].set_door(1,2,3,4);
    }
    Now the errors I get are:

    missing ';' before identifier 'Rooms'
    unexpected end of file found

    I tried editing it and moving it around. Am I just being a bit stupid and putting it in the wrong place or something?
    Last edited by Bean; 05-02-2004 at 09:54 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > extern CRoom Rooms[14]; // newly added
    Put this in rooms.h

    Then
    #include "rooms.h"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    3
    ah... I was being a bit stupid. Works perfectly now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM