C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2002, 06:30 PM
Beans4You's Avatar
Beans4You Beans4You is offline
Some day I will be a Lambda!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: NJ
Posts: 18 Beans4You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Eric

Reply With Quote
  #2  
Old October 11th, 2002, 07:47 PM
StealthElephant's Avatar
StealthElephant StealthElephant is offline
Shes dancing (obviously)
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2002
Location: the far side
Posts: 527 StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 38 m 39 sec
Reputation Power: 12
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?

Reply With Quote
  #3  
Old October 11th, 2002, 09:00 PM
Beans4You's Avatar
Beans4You Beans4You is offline
Some day I will be a Lambda!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: NJ
Posts: 18 Beans4You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #4  
Old October 12th, 2002, 01:38 AM
StealthElephant's Avatar
StealthElephant StealthElephant is offline
Shes dancing (obviously)
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2002
Location: the far side
Posts: 527 StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 38 m 39 sec
Reputation Power: 12
Re: Passing Pointers

Quote:
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);

Reply With Quote
  #5  
Old October 12th, 2002, 06:59 AM
Beans4You's Avatar
Beans4You Beans4You is offline
Some day I will be a Lambda!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: NJ
Posts: 18 Beans4You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb

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

Reply With Quote
  #6  
Old October 16th, 2002, 09:24 AM
pajo pajo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 0 pajo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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);
...
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Passing Pointers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap