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
Accessing classes before they are "made"
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.
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).
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;
};
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.
[edited by - Drakon on April 4, 2004 10:58:17 AM]
class A{public: Object ObjRef;};class Object{public: A ARef; };
[edited by - Drakon on April 4, 2004 10:58:17 AM]
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;};
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)
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)
Post your *actual* code (copy/paste). You can strip out irrelevant stuff like member functions, etc.
#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;};
I''m not sure why that doesn''t work, but I tried just flipping it around, and it works:
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement