The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Passing Pointers
Discuss Passing Pointers in the C Programming forum on Dev Shed. Passing Pointers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 11th, 2002, 06:30 PM
|
 |
Some day I will be a Lambda!
|
|
Join Date: Jun 2002
Location: NJ
Posts: 18
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
|

October 11th, 2002, 07:47 PM
|
 |
Shes dancing (obviously)
|
|
Join Date: Jul 2002
Location: the far side
Posts: 527
  
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
|

October 11th, 2002, 09:00 PM
|
 |
Some day I will be a Lambda!
|
|
Join Date: Jun 2002
Location: NJ
Posts: 18
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.
|

October 12th, 2002, 01:38 AM
|
 |
Shes dancing (obviously)
|
|
Join Date: Jul 2002
Location: the far side
Posts: 527
  
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);
|

October 12th, 2002, 06:59 AM
|
 |
Some day I will be a Lambda!
|
|
Join Date: Jun 2002
Location: NJ
Posts: 18
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
 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
|

October 16th, 2002, 09:24 AM
|
|
Junior Member
|
|
Join Date: Oct 2002
Posts: 0
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);
...
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|