Why am I getting this error?

This is a discussion on Why am I getting this error? within the C++ Programming forums, part of the General Programming Boards category; Below......

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Why am I getting this error?

    Below...
    Last edited by tallguy; 04-03-2007 at 08:31 PM.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Now I'm curious. What was the error and what caused it?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Ok I've got a new error now.

    Code:
    void Graph::printPath( const Vertex & dest ) const
    {
        if( dest.prev != NULL )
        {
            printPath( *dest.prev );
            cout << " was with ";
        }
        
        ActorDB::tag t = dest.a;
        
      cout << (*data)[t].name();     ----------Where I believe the problem is
    }
    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.

    Some of the other files...
    Graph.h...
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <list>
    #include <map>
    #include "GraphStuff.h"
    #include "ActorDB.h"
    
    struct Vertex;
    
    class Graph
    {
      public:
        Graph( ) { }
        Graph( vector<Actor> * d ) : data(d) { }
        ~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; }
    
    	vector<Actor> * data;
    
        vmap vertexMap;
    };
    ActorDB.h
    Code:
    #ifndef ACTORDB_H_
    #define ACTORDB_H_
    
    #include "Actor.h"
    
    #include <vector>
    
    /** 
     * 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_*/
    Last edited by tallguy; 04-03-2007 at 09:06 PM.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    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?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Where would the setActorList function to set the pointer go?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    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?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    Ok so in graph.h i have

    Code:
    class Graph
    {
      public:
        Graph( ) { }
        Graph( std::vector<Actor> * d ) : data(d) { }
        ~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);
        void setActorList(vector<Actor> * a);  ------------>new function
    and in graph.cpp i have

    Code:
    void Graph::setActorList(vector<Actor> * a)
     {
      data = a;
     }
    But I'm not sure where or how to call this function with the vector from the ActorDB class.

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    32

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    But I'm not sure where or how to call this function with the vector from the ActorDB class.
    In the constructor perhaps? Then you could have a constructor
    Code:
    Graph::Graph(vector<Actor> * a) {}
    BTW colours stand out better than lots of hyphens IMHO.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Quote Originally Posted by dwks View Post
    Then you could have a constructor
    Code:
    Graph::Graph(vector<Actor> * a) {}
    Actually, it was mentioned earlier in the thread that such a constructor won't work.

    tallguy, see my post #7 for information on where to call the function. Do you have any questions about those thoughts?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21