#1
  1. Some day I will be a Lambda!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    NJ
    Posts
    18
    Rep Power
    0

    Passing Pointers


    I'm trying to implement a BerkleyDB database. It works if I declare the Db inside a single function but it blows up on me if I try to implement it in a class and try to pass the Db to the function. The OS that I'm working on is Win2K and the compiler is VC++ 6.0. The BerkleyDB download and white pages can be found at www.sleepycat.com.

    Here is the code that works:
    Code:
    void createdb_pc(char* DATABASE_PC){
    	Db dbp_pc(0, 0);
    	dbp_pc.set_error_stream(&cerr);
    	dbp_pc.set_errpfx("AccessExample");
    	dbp_pc.set_pagesize(1024);		/* Page size: 1K. */
    	dbp_pc.set_cachesize(0, 32 * 1024, 0);
    	dbp_pc.open(NULL, DATABASE_PC, NULL, DB_BTREE, DB_CREATE, 0664);
    }
    Here is the code that I tried to get to work:
    Code:
    Private:
    Db dbDatabase(0, 0);
    
    void createdb_pc(Db *dbp_pc, char* DATABASE_PC){
     	dbp_pc.set_error_stream(&cerr);
    	dbp_pc.set_errpfx("AccessExample");
    	dbp_pc.set_pagesize(1024);		/* Page size: 1K. */
    	dbp_pc.set_cachesize(0, 32 * 1024, 0);
    	dbp_pc.open(NULL, DATABASE_PC, NULL, DB_BTREE, DB_CREATE, 0664);
    }
    
    Public:
    
    myClas(){
    	createdb_pc(dbDatabase, "data001.db");
    }
    Now I get a "Constant" error when on the dbDatabase(0, 0) and the compiler gives me another error stating that I need a struct on the right hand side set_error_stream, set_errpfx, set_page,...

    Any help would be greatly appreciated. :confused:

    Eric
  2. #2
  3. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    13
    why are u initializing db inside the class private segment?
    i haven't ever used that but i can tell straignt away that that definitely wont work

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?
  4. #3
  5. Some day I will be a Lambda!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    NJ
    Posts
    18
    Rep Power
    0
    I have another header file that needs to call this header to record the information that it gathers. All that I need is to have an instance of the Db class that I can pass into the function createdb_pc.

    My idea was to have the Db declared in this class so that the constructor would create-open the database and the destructor would close the database, and until the class is destroyed I can write to the database. I have also tried to create an instance of the class inside of another function and then pass that to createdb_pc, but it also crashes.
  6. #4
  7. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    13

    Re: Passing Pointers


    Originally posted by Beans4You

    Code:
    void createdb_pc(char* DATABASE_PC){
    	Db dbp_pc(0, 0);
    	dbp_pc.set_error_stream(&cerr);
    	dbp_pc.set_errpfx("AccessExample");
    	dbp_pc.set_pagesize(1024);		/* Page size: 1K. */
    	dbp_pc.set_cachesize(0, 32 * 1024, 0);
    	dbp_pc.open(NULL, DATABASE_PC, NULL, DB_BTREE, DB_CREATE, 0664);
    }
    Here is the code that I tried to get to work:
    Code:
    Private:
    Db dbDatabase(0, 0);
    
    void createdb_pc(Db *dbp_pc, char* DATABASE_PC){
     	dbp_pc.set_error_stream(&cerr);
    	dbp_pc.set_errpfx("AccessExample");
    	dbp_pc.set_pagesize(1024);		/* Page size: 1K. */
    	dbp_pc.set_cachesize(0, 32 * 1024, 0);
    	dbp_pc.open(NULL, DATABASE_PC, NULL, DB_BTREE, DB_CREATE, 0664);
    }
    
    Public:
    
    myClas(){
    	createdb_pc(dbDatabase, "data001.db");
    }

    Eric
    thats a syntax error when u try to do that with db, put that code inside a class method
    Db dbDatabase(0, 0);

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?
  8. #5
  9. Some day I will be a Lambda!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    NJ
    Posts
    18
    Rep Power
    0

    Lightbulb


    :rolleyes: I'm sorry for wasting everyone's time. I got some sleep and then looked at it and wanted to slap myself. I don't need to worry about the Db handle, I just need to pass in the name of the database file, the key, and data field. It's to easy... I was making it way to hard. Thanks for everyone's help. Here is the code if anyone would like to see.

    Code:
    void dbWrite(char* DATABASE, char* KEY,char* DATA){
    	Db dbp(0, 0);
    	int ret;
    
    	dbp.set_error_stream(&cerr);
    	dbp.set_errpfx("AccessExample");
    	dbp.set_pagesize(1024);		/* Page size: 1K. */
    	dbp.set_cachesize(0, 32 * 1024, 0);
    	dbp.open(NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664);
    
    	Dbt key(KEY,sizeof(KEY));
    	Dbt data(DATA,sizeof(DATA));
    	
    	ret = dbp.put(0, &key, &data, DB_NOOVERWRITE);
    	if (ret == DB_KEYEXIST) {
    		cout << "Key " << KEY << " already exists.\n";
    	}
    	
    	dbp.close(0);
    }
    Thanks again.

    Eric
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    0
    Rep Power
    0
    In your code:

    void createdb_pc(Db *dbp_pc, char* DATABASE_PC){
    dbp_pc.set_error_stream(&cerr);
    ...
    }

    you pass pointer to Db (dbp_pc), and dereference it as a class (dot member). You shoud either pass buy reference:

    void createdb_pc(Db& dbp_pc, char* DATABASE_PC){
    dbp_pc.set_error_stream(&cerr);
    ...
    }

    or use -> to dereference:

    void createdb_pc(Db *dbp_pc, char* DATABASE_PC){
    dbp_pc->set_error_stream(&cerr);
    ...
    }

IMN logo majestic logo threadwatch logo seochat tools logo