i use C++, Berkeley DB 18.1.40 STL and Visual Studio 2015. i include libraries through the project settings i cant use dbstl::map<string, int> and get an error, but with dbstl::map<string, string> everything works
My code:
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "libdb181.lib")
#pragma comment(lib, "libdb_stl181.lib")
#include "stdafx.h"
#include <iostream>
#include <dbstl_map.h>
#include <vector>
using namespace std;
int main(){
const char* db_name = "db";
const char* db_home_dir = "dirdb";
DbEnv env(DB_CXX_NO_EXCEPTIONS);
Db* pdb;
try {
dbstl::dbstl_startup();
env.set_error_stream(&cerr);
env.open(db_home_dir, DB_CREATE | DB_INIT_MPOOL, 0);
pdb = new Db(&env, DB_CXX_NO_EXCEPTIONS);
pdb->open(NULL, db_name, NULL, DB_BTREE, DB_CREATE, 0);
/* STRING */
typedef dbstl::db_map<string, string> dbmap_string;
dbmap_string map_string(pdb, &env);
string key_string = "string";
string value_string = "its string!";
map_string.insert({ key_string, value_string });
string outvalue_string = map_string[key_string];
cout << "key: " << key_string << endl << "value: " << outvalue_string << endl << endl;
/* INT */
typedef dbstl::db_map<string, u_int32_t> dbmap_int;
dbmap_int map_digit(pdb, &env);
string key_digit = "digit";
u_int32_t value_digit = 5;
map_digit.insert({ key_digit, value_digit });
dbstl::dbstl_exit();
if (pdb != NULL) {
pdb->close(0);
delete pdb;
}
env.close(0);
}
catch (DbException& e) {
cerr << "DbException: " << e.what() << endl;
return -1;
}
catch (std::exception& e) {
cerr << e.what() << endl;
return -1;
}
cout << "hello";
_sleep(5000);
return 0;
}
i get error:
Error C2516 'ddt': is not a legal base class ConsoleApplication8 c:\users\kordi\desktop\db18.1.40\lang\cxx\stl\dbstl_element_ref.h 58
1>c:\users\kordi\desktop\db-18.1.40\lang\cxx\stl\dbstl_element_ref.h(58): error C2516: 'ddt': is not a legal base class
1> C:\Users\kordi\Desktop\db-18.1.40\lang\cxx\stl\dbstl_map.h(727): note: see declaration of 'ddt'
1> C:\Users\kordi\Desktop\db-18.1.40\lang\cxx\stl\dbstl_map.h(727): note: see reference to class template instantiation 'dbstl::ElementRef<ddt>' being compiled
1> with
1> [
1> ddt=u_int32_t
1> ]
1> C:\Users\kordi\Desktop\db-18.1.40\lang\cxx\stl\dbstl_map.h(1148): note: see reference to class template instantiation 'dbstl::db_map_iterator<kdt,ddt,value_type_sub>' being compiled
1> with
1> [
1> kdt=std::string,
1> ddt=u_int32_t,
1> value_type_sub=dbstl::ElementRef<u_int32_t>
1> ]
1> ConsoleApplication8.cpp(55): note: see reference to class template instantiation 'dbstl::db_map<std::string,u_int32_t,dbstl::ElementRef<ddt>,dbstl::db_map_iterator<kdt,ddt,value_type_sub>>' being compiled
1> with
1> [
1> ddt=u_int32_t,
1> kdt=std::string,
1> value_type_sub=dbstl::ElementRef<u_int32_t>
1> ]
why am i getting this error? can i fix it?
You ended up using an ElementRef
instead of an ElementHolder
in the u_int32_t
case.
ElementHolder
class is used to store primitive types [like u_int32_t
, my edit] into STL containers"ElementRef
class is used to store other types into STL containers".Inheriting from u_int32_t
like the ElementRef
does in your case is never going to work. It should have been an ElementHolder<u_int32_t>
.
You need to change your map containing a primitive type:
typedef dbstl::db_map<string, ElementHolder<u_int32_t>> dbmap_int;
// ^^^^^^^^^^^^^^^^^^^^^^^^
This also seems in line with the examples and documentation @ docs.oracle.com
The
ElementHolder
class template should be used for every type ofdbstl
container that will store C++ primitive data types, such asint
,float
,char *
,wchar_t *
, and so forth.