CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    31

    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.

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    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

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How to fix this?

    Quote 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.
    Quote 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?

  4. #4
    Join Date
    Feb 2007
    Posts
    31

    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.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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).

  6. #6
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    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
    }

  7. #7
    Join Date
    Feb 2007
    Posts
    31

    Re: How to fix this?


  8. #8
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    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

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured