|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 |
|
#2
|
||||
|
||||
|
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? |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
Re: Passing Pointers
Quote:
thats a syntax error when u try to do that with db, put that code inside a class method Db dbDatabase(0, 0); |
|
#5
|
||||
|
||||
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 |
|
#6
|
|||
|
|||
|
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); ... } |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Passing Pointers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|