A function in a class to pass a reference to that class

Started by
3 comments, last by wizard341 21 years, 1 month ago
This may sound kind of wierd, so dont ask why im doing it this way. I need to have a vector to hold a bunch of objects. So here is what i do, in my main file i have this definition. typedef vector PLAYERVECTOR; ,where CPlayer is a class which of course works fine, since ive included CPlayer.h in main. Now later on i need to pass that vector BACK to a function in CPlayer, so it can check through the vector for values and do some calculations internally. The only problem is, i cant define a prototype of that function in the class. ex. i cant have public CPlayer { foo(PLAYERVECTOR &PV) } Because of course CPlayer dosnt know what PLAYERVECTOR is. So i tried to define the typedef in CPLayer, but then again i cant define the vector before the class definition, ex. typedef vector PLAYERVECTOR; public CPlayer { foo(PLAYERVECTOR &PV) } because then it dosnt know what CPLayer is, and i cant do it afterwards ex. public CPlayer { foo(PLAYERVECTOR &PV) } typedef vector PLAYERVECTOR; because i run into the same problem as before as not being able to put PLAYERVECTOR as a paramater. Anyone have a few ideas on what i could be doing to make this work, or maybe go through a differnt idea that would let me store a set of objects into some sort of container but still let the class access that container and all the classes inside of it?
Advertisement
You can have a vector of pointers to CPlayers.
Your problems might go away if you did

class CPlayer;
typedef std::vector<CPlayer> PLAYERVECTOR;

class CPlayer {
foo(PLAYERVECTOR &);
};

Technically, this is illegal C++, but it works on most compilers anyway.

For legal C++ you''d have to use:

class CPlayer;
typedef std::vector<CPlayer *> PLAYERVECTOR;

class CPlayer {
foo(PLAYERVECTOR &);
};
In what way is the first example illegal?

Regards Mats

[edited by - Matsen on March 7, 2003 8:54:50 PM]
Section 17.4.3.6 Paragraph 2: "In particular, the effects are undefined ... if an incomplete type ... is used as a template argument when instantiating a [standard library] template component."

In this case CPlayer being an incomplete type in the instantiation of std::vector.

This topic is closed to new replies.

Advertisement