Accessing classes before they are "made"

Started by
12 comments, last by Drakon 20 years ago
I need to make references to a class B within class A, but class B is made after class A so it has no idea what class B is when I want it.


class A
{
public:
      Object   ObjRef;
};

class Object
{
public:
      int Blah;
      Object   IWish;
}
Also, how can I make a reference to its own class, like the IWish thing there? That causes this to happen: c:\vc++ projects\objhandler\classes.h(24) : error C2460: ''IWish'' : uses ''Object'', which is being defined
"Go for the eyes, Boo, go for the eyes!"
Advertisement
Object *IWish;
1. You cant use Object before it''s been declared, so switch the order on the two classes.
2. If you want to access it like this you have to declare a pointer to it. It would be impossible to declare the same object in the same class since it would have to create an "Object" for each "Object" created (will recurse in all infinity).
class Object{public:  int Blah;  Object *  IWish;};class A{public:  Object   ObjRef;}; 
I should have complicated it... This is a more accurate description of what I have. Each class references each other.

class A
{
public:
Object ObjRef;
};

class Object
{
public:
A ARef;
};
I should have complicated it... This is a more accurate description of what I have. Each class references each other.

class A{public:      Object   ObjRef;};class Object{public:         A        ARef;      };


[edited by - Drakon on April 4, 2004 10:58:17 AM]
"Go for the eyes, Boo, go for the eyes!"
If you want them both in the same file, just declare the class you need, and define it later:

class Object; // now class A and class Object can both use Objectclass A{public:      Object   ObjRef;};class Object{public:      int Blah;      Object   IWish;}; 
Brianmiserere nostri Domine miserere nostri
it says:

c:\vc++ projects\objhandler\classes.h(27) : error C2079: ''RoomContainer'' uses undefined class ''Room''

(not using those same classes in my example... I use Object and Room)
"Go for the eyes, Boo, go for the eyes!"
Post your *actual* code (copy/paste). You can strip out irrelevant stuff like member functions, etc.
Brianmiserere nostri Domine miserere nostri
#pragma once#include <cstring>#include <string>#include <sstream>using namespace std;class Room;class Object{public:	int		ONum;	int		Hierarchy;	vector<int>	Item;	vector<int> ItemHierarchy;	vector<Object> OItem;	int		Container;	int		ContainerHierarchy;	Object  *ObjectContainer;	Room	RoomContainer;	bool	ContainerIsRoom;};class Room{public:	int		RNum;	vector<int>	Item;	vector<Object> OItem;};

"Go for the eyes, Boo, go for the eyes!"
I''m not sure why that doesn''t work, but I tried just flipping it around, and it works:

class Object;class Room{public:	int		RNum;	vector<int>	Item;	vector<Object> OItem;};class Object{public:	int		ONum;	int		Hierarchy;	vector<int>	Item;	vector<int> ItemHierarchy;	vector<Object> OItem;	int		Container;	int		ContainerHierarchy;	Object  *ObjectContainer;	Room	RoomContainer;	bool	ContainerIsRoom;};


However, you may want to rethink your design anyway: an object contains a room? And a room contains a list of objects, each of which contains a room?
Brianmiserere nostri Domine miserere nostri

This topic is closed to new replies.

Advertisement