-
April 3rd, 2007, 10:50 PM
#1
How to fix this?
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?
Here is my Graph.h file....
Code:
class Graph
{
public:
Graph( ) { }
Graph( std::vector<Actor> * d ) : data(d) { } ----->Constructor I want
~Graph( );
void addEdge( const ActorDB::tag & sourceName, const ActorDB::tag & destName, double cost );
void printPath( const ActorDB::tag & destName ) const;
void unweighted( const ActorDB::tag & startName );
//void dijkstra( const std::string & startName );
void addVertex (const ActorDB::tag & startName);
private:
Vertex * getVertex( const ActorDB::tag & vertexName );
void printPath( const Vertex & dest ) const;
void clearAll( );
typedef map<int ,Vertex *,less<ActorDB::tag> > vmap;
// Copy semantics are disabled; these make no sense.
Graph( const Graph & rhs ) { }
const Graph & operator= ( const Graph & rhs )
{ return *this; }
std::vector<Actor> * data;
vmap vertexMap;
};
This is the Movie class.....
Code:
class Movie
{
public:
Movie();
/**
* Build a new movie.
*
* @param t The title of the movie
* @param y The year string for this movie
* @param a A pointer to the ActorDB object. The movie stores actor tags
* taken from this object.
*
*/
Movie(const std::string& t, const std::string& y, ActorDB* a = 0);
/// return the title of the movie
std::string title() const;
/// returns the year in which the movie was made.
std::string year() const;
/**
* Add an actor to the movie's cast list. Takes a tag from the actor database
* as input. Note that it's marked "const" -- the cast list does not participate
* in the ordering of Movies; two Movies are logically equal if their titles and years
* are equal. Thus, the cast list is a mutable member of the Movie.
*
* @param a a tag referring to an actor to add to the list. To dereference such a tag,
* we have to pass it back to the ActorDB object it came from.
*/
void add_actor(ActorDB::tag a) const;
/**
* Returns a vector of Actors, the cast of this movie.
*
* @return a vector of copies of the Actors with roles in this movie.
*/
std::vector<Actor> cast() const;
void actorConnections(ActorDB::tag a) const;
void printPath(const ActorDB::tag & name1, const ActorDB::tag & name2);
private:
/// A pointer to the ActorDB that stores the actors with roles in this movie.
/// Used to resolve the ActorDB::tag objects that we store.
ActorDB* adb;
/// The title of this movie.
std::string the_title;
/// The year in which this movie was released.
std::string the_year;
/// A vector of ActorDB::tag objects, referring to the Actor objects
/// stored in *adb that represent our cast.
mutable std::vector<ActorDB::tag> the_cast;
///The private graph for the Kevin Bacon Problem
static Graph actorGraph(adb->pointer());
};
Last edited by Ehump20; April 3rd, 2007 at 10:54 PM.
-
April 3rd, 2007, 11:10 PM
#2
Re: How to fix this?
AFAIK you can't initialise static members inside the class. You need to have
Code:
class Movie
{
...
static Graph actorGraph;
}
Graph Movie::actorGraph = <initial value>;
Remember that you cannot change the static once assigned and it can only be referenced by static member functions.
What you probably want is a singleton, something like
Code:
class Movie
{
...
static Graph *pInstance;
static Graph Instance();
}
Graph * Movie::pInstance = null;
Graph * Movie::Instance()
{
if ( pInstance == null )
pInstance = new Graph(adb->pointer());
return pInstance;
}
Regards
Alan
-
April 4th, 2007, 04:56 AM
#3
Re: How to fix this?
Originally Posted by AlanGRutter
AFAIK you can't initialise static members inside the class. You need to have
It is not initialization that requires static members to be written outside class definition but that is the way you need to do to define them. Initialization can be done while defining.
Originally Posted by AlanGRutter
Remember that you cannot change the static once assigned and it can only be referenced by static member functions.
Why can you not change a static variable once assigned? And why can they only be referenced by static member functions only?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 4th, 2007, 06:49 AM
#4
Re: How to fix this?
Ok so I put in my Movie.h file
Code:
static Graph *actorGraph;
static Graph actorGraph();
and in my Movie.cpp file
Code:
Graph * Movie::actorGraph = null;
Graph * Movie::actorGraph()
{
if ( actorGraph == null )
actorGraph = new Graph(adb->pointer());
return actorGraph;
}
But I get errors.
-
April 4th, 2007, 06:55 AM
#5
Re: How to fix this?
Can you post a latest fresh piece of compilable code that you have? What are the errors that you get? You don't put those statics in the .h file but in an implementation file (.cpp etc).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 4th, 2007, 09:20 AM
#6
Re: How to fix this?
you're getting errors because you didn't put static in front of your actorGraph() definition in the cpp.
Code:
static Graph *Movie::actorGraph()
{
// blah blah blah
}
-
April 4th, 2007, 02:41 PM
#7
-
April 4th, 2007, 05:11 PM
#8
Re: How to fix this?
Exterminator - the below description is what I was hinting at
An important detail to keep in mind when debugging or implementing a program using a static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead. Moreover, you are required to initialize the static class member or it will not be in scope. (The syntax is a bit weird: "type class_name::static_variable = value".)
You can also have static member functions of a class. Static member functions are functions that do not require an instance of the class, and are called the same way you access static member variables -- with the class name rather than a variable name. (E.g. a_class::static_function(); rather than an_instance.function() Static member functions can only operate on static members, as they do not belong to specific instances of a class. Static member functions can be used to modify static member variables to keep track of their values -- for instance, you might use a static member function if you chose to use a counter to give each instance of a class a unique id.
Regards
Alan
-
April 5th, 2007, 12:02 AM
#9
Re: How to fix this?
Initialization is not necessary in your syntax for static definition in an implementation file, in fact statics are automatically default initialized. Static member can be accessed via object syntax. They can be changed in non-static member functions as well. I guess the following code shows all those cases. What you mentioned in your previous post, hence, was incorrect.
Code:
class A
{
public: static int i;
public: static void f(){++i;}
public: void g(){++i;}
};
int A::i;
int main()
{
A a;
++(A::i);
++(a.i);
a.f();
a.g();
A::f();
}
Ehump20, can you write a small test program that duplicates your error? Not everyone has the time to look into that much code.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|