C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2004, 09:08 PM   #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

}
Bean is offline   Reply With Quote
Old 05-02-2004, 09:14 PM   #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
Zach L. is offline   Reply With Quote
Old 05-02-2004, 09:29 PM   #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.
Bean is offline   Reply With Quote
Old 05-03-2004, 12:32 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,630
> 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.

Salem is offline   Reply With Quote
Old 05-03-2004, 11:22 AM   #5
Registered User
 
Join Date: May 2004
Posts: 3
ah... I was being a bit stupid. Works perfectly now. Thanks!
Bean is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
question about multidimensional arrays richdb C Programming 22 02-26-2006 09:51 AM
A question about constructors... Wolve C++ Programming 9 05-04-2005 04:24 PM
Merge sort please vasanth C Programming 2 11-09-2003 12:09 PM
Creating an array of object pointers Unregistered C++ Programming 2 10-08-2001 10:01 PM
Bitmap from array to an object steve_i83 Windows Programming 2 09-28-2001 08:16 AM


All times are GMT -6. The time now is 09:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22