I am trying to store actors as vertices for the Kevin Bacon Game. But for each actor I stored an integer which is an index in the vector that contains the actors. However, when I try to print each actor in the function above, I'm sure I am not accessing the actor in the vector correctly the way I have done it because the program terminates.

/**
* This object holds all the actor objects in our program in exactly one place.
* When you add an actor to the database, you get a "tag" back. Keep that tag,
* so that when you want the actor again, you can get it. Tags are small, and
* lookup is fast (constant time).
*/
class ActorDB
{
public:
/// The abstract type used by clients to reference the stored Actor objects.
typedef unsigned int tag;

/**
* Create an empty database.
*/
ActorDB();

/**
* Destroy the database and all of the actors in it. All tags are now, of course,
* invalid.
*/
~ActorDB();

/**
* Add an actor to the database, and get a tag back that refers to it. Runs in
* constant time (amortized). Note that this function does not check for duplicates
* before inserting, since in common applications this isn't necessary.
*
* @param a The actor to be stored. A copy will be placed in the database.
* @return a tag object that can be used to get the stored Actor.
*/
tag add_actor(const Actor& a);

/**
* Retrieve a reference to the stored Actor based on a tag. Runs in constant time.
*
* @param t a tag referring to some entry in our database
* @return a constant reference to the stored Actor.
*/
const Actor& get_actor(tag t) const;

unsigned int getTag(const Actor & a) const;

private:
std::vector<Actor> data;
};

#endif /*ACTORDB_H_*/

04-03-2007

tallguy

Ok I believe my problem is that when I create my Graph object I am not passing it the correct reference to the vector.

Code:

static Graph actorGraph;

That is how I'm currently calling it which calls the default destructor.

But I need to pass it a reference of the vector stored in my ActorDB class. So I created a function in my ActorDB.cpp class to return the reference to the vector:

Code:

vector<Actor> * ActorDB::pointer()
{
return &data;
}

But now I don't know how to access this funtion from my Movie class, which is where I create my graph. I tried calling it with:

Code:

static Graph actorGraph(adb->pointer());

But that's just an error. Anyone know how I can call this function from my Movie class?

04-03-2007

Daved

The vector in your Actor class is not static, meaning one will only be created when you create an ActorDB instance. This is fine, if you have an ActorDB instance somewhere in your program. However, the Movie class's actorGraph is static, so it is created at the start of your program, most likely before the ActorDB instance. So you cannot pass the pointer to it. If you could, you would do it in the cpp file where you define the actorGraph:

Code:

Graph Movie::actorGraph(adb->pointer());

I think a better idea would be to use the default constructor for the graph and then have a setActorList (or whatever) function to actually set the pointer and do all the construction work later.

04-03-2007

tallguy

Where would the setActorList function to set the pointer go?

04-03-2007

Daved

In the Graph class. It would do the same thing the Graph( vector<Actor> * d ) constructor does, which is set the data pointer to d.

If you meant where would you call it, I would call it as soon as adb is created. You can do that in the adb constructor, or in regular code just after that variable is created. I'm not sure which is better, it may not matter.

If the actorGraph is private to Movie, then you may have to make another function in Movie similar to setActorList which just calls the actorGraph's setActorList. This might be necessary since the private actorGraph wouldn't be accessible outside the Movie class.

04-03-2007

tallguy

Ok so in graph.h I put

Code:

std::vector<Actor> * setActorList();

and in graph.cpp I put

Code:

std::vector<Actor> * Graph::setActorList()
{
return &data;
}

I don't think this is right?

04-04-2007

Daved

>> I don't think this is right?
I don't think its right either. That is how you would write a getActorList() function. You want a setActorList() function. The second Graph constructor is essentially exactly what you want, except it has no return type because it is a constructor. Just change the name and make it void, then save the pointer inside the function.